Minimum jump to reach end

Поделиться
HTML-код
  • Опубликовано: 24 янв 2025

Комментарии • 156

  • @baviskarakshay
    @baviskarakshay 4 года назад +122

    Captions Autogenerator: My name is too sharp😂😂

  • @nmreads
    @nmreads 4 года назад +6

    Thank you very much. I started my day working on this question and I spent an hour and did not understand anything from various solutions that I looked into. I was frustrated and suddenly I found your video. I completely got it now, I am really thankful to you.

  • @SoumyajitGanguly_ALKZ
    @SoumyajitGanguly_ALKZ 8 лет назад +60

    This is O(n^2). An easier to understand O(n^2) solution would be to traverse the array backwards and keep filling in values. Define new array as arr[n]. arr[last] will be 0. If we are at position i and input[i] = 5 we need to check the next 5 values in our arr and put arr[i] = min(all those values). Do this 1 by 1 backwards. Return arr[0].

    • @rishipalbhatia
      @rishipalbhatia 6 лет назад +1

      Soumyajit Ganguly yes this is the more intuitive solution too.

    • @sachinchandwani4085
      @sachinchandwani4085 4 года назад +1

      I thought that too. But problem comes in the solutions where you didn't have to make maximum length jump. Hence it got TLE in this solution.

    • @mansiagarwal3677
      @mansiagarwal3677 4 года назад

      @@sachinchandwani4085 Can you please elaborate on what you are trying to say. Actually, I also thought of the solution mentioned by. I would love to know the cases where that could fail.

    • @sachinchandwani4085
      @sachinchandwani4085 4 года назад +2

      MANSI AGARWAL I’ve tried this logic. if array is of all 1’s then you’d get TLE. And about my comment- i was saying that if solution contains jump where you don’t have to make jump of maximum length then you’d have to try all possible jumps which will take more time too

    • @derilraju2106
      @derilraju2106 2 года назад

      @@mansiagarwal3677
      class Solution:
      def jump(self, nums: List[int]) -> int:
      if len(nums) ==1: return 0
      dp = [0 for _ in range(len(nums))]
      for i in range(len(nums)-2,-1,-1):
      temp = float("inf")
      for j in range(1,nums[i]+1):
      if i+j < len(nums):
      temp = min(temp, dp[i+j] )
      dp[i] = temp+1
      return dp[0]

  • @priyanshiagarwal7032
    @priyanshiagarwal7032 4 года назад +40

    I just want to know how do you get intuition of these dp approaches.

    • @shivanshutiwari5646
      @shivanshutiwari5646 4 года назад +38

      when my solution gives TLE then I get an intuition of DP approach.

    • @harichandana9891
      @harichandana9891 4 года назад +2

      Exactly 😭

    • @amey203
      @amey203 4 года назад +5

      Basically think step by step. Like for this problem. Think as for array len 1 ,then for 2 .. so on

    • @SHIVAPRASAD-wj6tg
      @SHIVAPRASAD-wj6tg 4 года назад +6

      DP is nothing but careful bruteforce so we are simply trying all possibilities but carefully keep this in mind and build recursion tree and store subproblems

    • @faizankhan-eq7ev
      @faizankhan-eq7ev 4 года назад +7

      Forget everything, solve it with brute force , observe the pattern , if code is getting executed multiple times with same variables , that means you somehow need to avoid that extra executions , and that's where you think of optimizing the solution.
      DP is not the only solution, it helps in optimization.

  • @anirudhdevpura2798
    @anirudhdevpura2798 3 года назад +10

    Tushar Roy: "yes..we will use dp to solve this problem"
    Me: But why??

    • @viditsinghbrahmania1476
      @viditsinghbrahmania1476 3 года назад +3

      Exactly my point brother. Moreover O(n^2) solutions aren't even accepted.

  • @OtakRajCodes
    @OtakRajCodes 2 года назад +1

    Excellent explanation . absolutely loved the blackboard style teaching.

  • @MrRichiban
    @MrRichiban 2 года назад +2

    Lovely explanation Tushar!
    In fact, you don't need the second array; it's possible to reconstruct the jumps you took simply from the jump array.
    For example, let `i` = 9 (the last index of our array). At `jumps[9]` we have a value of 4. You know then that you must have got here from an index with a jump number of 3. So you can send another pointer `j` back through `arr` and `jumps` until you find an index such that `jumps[j]` equals 3 and `arr[j]` is sufficiently large to reach index `i`. When you have such an index `i` you can safetly conclude that this is one of your jumps, so add it to your output.
    Repeat the process until you eventually reach index 0.

  • @nicolaschataing1457
    @nicolaschataing1457 5 лет назад +1

    The greedy solution works here, yielding a O(n) time / O(1) space : when you're at index i, you just optimize the next jump by taking the index max (i < j

    • @brainfreeze192
      @brainfreeze192 4 года назад

      can u share the code pls!

    • @NitinPal101
      @NitinPal101 4 года назад

      @@brainfreeze192 github.com/Errichto/youtube/blob/master/leetcode/april-2020-challenge/25-jump-game.cpp

  • @techieinformation4701
    @techieinformation4701 9 лет назад +7

    I guess we can optimize this a little bit by analyzing the actual jump array.. instead of starting i from 0, we can check the actual jump array of previous element and start i from that index. for e:g in your array at 5th index, the value is 4, here we can check the previous element i.e 2 which is at index 4 and its actual jump index, if that value required 2 jumps then there is no way that the index 5 can be reached from index 0 directly.So, instead we can start i from the previous element's actual jump array.

  • @nldanand
    @nldanand 6 лет назад +8

    for 1st example you are jumping to next element which is from 2 to 3 and from 2 to 4 (1 element jump). Same for 3 your jumping 3 elements !!! why for 2 only 1 element jump ??

    • @anilantony2068
      @anilantony2068 3 года назад

      It is not mandatory to make the maximum possible jumps from one point. The requirement is to keep the total jumps minimum. So, if u make 2 jumps from there u r landing in 2 and subsequently u make another 2 jumps to land in 1 and then another jump to reach the final point. Finally, it would results in 5 jumps in total instead of 4 which is the minimum one.

    • @gauravabhishek3930
      @gauravabhishek3930 3 года назад

      @@anilantony2068 Thats why I thought everyone is wrong!, GOT it. :v

  • @sumeetvandakudri9784
    @sumeetvandakudri9784 7 лет назад +10

    Hey Tushar, you are doing a great job!! just a thought. Can you please explain how to decide, if a problem can be solved with DP approach. Thanks.

    • @SriramGopalGoli030792
      @SriramGopalGoli030792 6 лет назад

      If a problem has optimal substructure and overlapping subproblems, then either DP or Greedy solution may apply. This is explain in detail in the CLRS book.

    • @sureshchaudhari4465
      @sureshchaudhari4465 4 года назад +2

      this guy just follows his algo without telling us why he formulated that algorithm

    • @m.e.t.a.l3125
      @m.e.t.a.l3125 3 года назад +1

      ​@@sureshchaudhari4465 Just put some efforts of your own too. ​

  • @shashankdubey7461
    @shashankdubey7461 4 года назад

    We can use graph over here make a connection of indexes will be based on jump given and then we can use dfs bfs anything and check whether we have visited that node or not during traversal .

  • @sharathnagendra3318
    @sharathnagendra3318 3 года назад +2

    Tushar: "Yes, we'll use Dynamic Programming to solve this problem"
    Me(Beginner): "Why the fuck he is choosing DP? Care to elaborate?" :|

  • @avinashkhetri9575
    @avinashkhetri9575 4 года назад +7

    Could you explain the O(n) approach as well? thank you

  • @shikharbhatia595
    @shikharbhatia595 9 лет назад +1

    Awesome sir! I have viewed a couple of your lectures on dynamic programming and i find the explanation really nice and to the point . Thanks a lot! :)

  • @tsaideepak8762
    @tsaideepak8762 5 лет назад +8

    simpler solution:
    l=[2,4,1,7]
    jumps=0
    end=0
    farthest=0
    for i in range(len(l)-1):
    farthest=max(farthest,i+l[i])
    if(i==end):
    jumps+=1
    end=farthest
    print(jumps)

  • @ekaanshkhosla
    @ekaanshkhosla 7 лет назад +5

    Can we use Bfs for this like in Snake and ladder problem to find min steps to reach the Goal?

  • @Star_Bawa9
    @Star_Bawa9 2 года назад +2

    I kindof understood nothing ...

  • @TheTutorialspoint
    @TheTutorialspoint 9 лет назад +5

    i am very thankful 2 you...i was struggling to learn DP but now i will able to understand and solve dp question from your video...if possible provide similar questions on codechef or hacker eerth...and keep making lots of vids

  • @divyanshruhela
    @divyanshruhela 4 года назад +1

    bhai tu GOD hai Massive Respect

  • @shivanshutiwari5646
    @shivanshutiwari5646 4 года назад +2

    everything is temporary but watching video of tushar Roy after stuck in DP problem is permanent.(100th comment is mine)

  • @vasy4321
    @vasy4321 8 лет назад +3

    - problem desciption -
    So how do we solve this?

    • @HarishAmarnath
      @HarishAmarnath 4 года назад

      yes, we will use dynamic programming to solve this

  • @MrRys
    @MrRys 7 лет назад +1

    do we always need to set the j back to 0? wouldn't it be enough to set j to the last number in the actual jump array?

  • @KanagaveluSugumar
    @KanagaveluSugumar 9 лет назад +1

    Why DP is required here?, Just we have to pick max jump value with in the given possibilities e.g) on given array 3,4,3,3,... the possibilities are 4,3,3 and we have to choose the second 3 which will give us the next maximum possibilities rit?

    • @KanagaveluSugumar
      @KanagaveluSugumar 9 лет назад

      second 3 because MAX(4 - 2, 3 - 1, 3 - 0) which second three.

  • @sujoyseal195
    @sujoyseal195 3 года назад

    I just wanted to know what is the logic behind moving of i and j ? In , other words you should explain when are we actually backtracking j and when we are incrementing i . You blindly explained the dry run of the algorithm instead of explaining the intuition which led to the formation of the algorithm.

  • @jiyanaadlakha
    @jiyanaadlakha 9 лет назад

    great video:) the way u depict how the matrix or array gets filled solves everything for me.. writing the code becomes cakewalk thnks :)

  • @PriyankaPatel-gx3pj
    @PriyankaPatel-gx3pj 4 года назад

    You always makes dp easy for us

  • @saketbhojane5697
    @saketbhojane5697 7 лет назад

    Hey Tushar,
    Can the above Qn be solved with this algorithm?
    Step 1: We've an array of the same size as the no of elements in the array signifying no of jumps it takes to reach the last element. Hence, we initialize it with infinity initially and mark the last cell as 0 since the last element can reach itself with 1 jump.
    Step 2: We see what all elements can reach the last element with 1 jump, then update those element's cell as #jumps to reach the last element (which will be zero initially by step 1).
    Step 3: We see if the first element is reachable with a value less than infinity. If yes terminate else we mark the nearest element to the first element as the last element and goto step 2.
    Tell me if there's anything wrong with this algorithm. Thanks.

  • @shivprakashsharma9511
    @shivprakashsharma9511 7 лет назад +2

    Sir , Can you explain one thing,If we add a constraint in the question that is we need to visit every point exactly once and then ,if we ask In how many ways you can reach at the end ,than what will be the answer

  • @lylescottiii3441
    @lylescottiii3441 9 лет назад

    In the code sample on github, shouldn't "for(int j=0; j < arr.length; j++){" have the middle condition of "j < i" ?

  • @lylescottiii3441
    @lylescottiii3441 9 лет назад +1

    Thanks for your video, great stuff

  • @Life2Experience
    @Life2Experience 5 лет назад

    Awesome explaination!!! Keep up the great work buddy

  • @chintamadhu001
    @chintamadhu001 9 лет назад

    Hello Tushar, At index 4 value is 2 so it should be two jumps and not 4 jumps right ?

  • @shivamtaparia9948
    @shivamtaparia9948 4 года назад

    what if the value at index 0 was 0 then it wouldnt have been possible to reach the end.Likewise there are lots of possibilities when we cant get any solution. Could you add that part too

  • @SritharRamadoss
    @SritharRamadoss 7 лет назад

    Nice Video Tushar !!! Thanks a lot for taking time and sharing your knowledge.

  • @ajinkyakale8941
    @ajinkyakale8941 9 лет назад

    In the no of jumps array at index 3, while calulating min no. of jumps to reach 3 from 1, won't the number of jumps required be 3, since min no. of jumps to reach 1 + min no. of jums req from 1 to 3 i.e(1+2)?

  • @mayankagarwal7016
    @mayankagarwal7016 7 лет назад +2

    you should have told naive approach for all the questions before the dp approach

  • @anonymousvoid6356
    @anonymousvoid6356 2 года назад +1

    yes we will use dynamic programming to aolve this qn.
    EPIC

  • @ayushjindal4981
    @ayushjindal4981 4 года назад +4

    Best solution is in O(N).

  • @mohammedrafeeq4902
    @mohammedrafeeq4902 6 лет назад

    can't we find the max of the elements of subarray which we can jump and jump to that position

  • @genki316
    @genki316 5 лет назад

    Is there a way to trace your steps using only the solution array? I know you can with some DP problems. Was wondering if this is possible for this problem.

  • @aritrakumarbara5994
    @aritrakumarbara5994 3 года назад

    Very well explanation sir🙏

  • @imyounick
    @imyounick 2 года назад

    What if we have -ve number ?

  • @arindam..9486
    @arindam..9486 9 лет назад

    Great video.Need more on dynamic programming with some code descriptions.

  • @singvijaya
    @singvijaya 9 лет назад

    I am thoroughly enjoying your explanations.. Thanks a lot.. Great work.. Spell correction: Mimimum -> Minimum

  • @shrimpo6416
    @shrimpo6416 2 года назад

    I think we can also use BFS to find min jumps.

    • @sranil
      @sranil 2 года назад +1

      Yes, that would be the best approach to solve this problem.

  • @arvindkumar95
    @arvindkumar95 9 лет назад

    Can you please give a solution to dis {1,0,5,8,0,0,6,0,0,8,9}
    The answer should be -1 since we cannot reach the end. But how to achieve it using the program you gave. I'm getting the value of MAX_INT... how to edit it according to this case.
    Thanks in advance.

  • @pnachtwey
    @pnachtwey 7 лет назад

    Simple, do a recursive search. Keep track of the fewest numbers of jumps found so far. Don't search if more from that point is the number of jumps exceeds the fewest so far.

  • @arnavkarforma3015
    @arnavkarforma3015 4 года назад

    Nice solution, but can be solved in a lot more simpler way
    class Solution {
    public int jump(int[] nums) {
    if(nums.length == 1) return 0;
    int res = 0; int currJump =0; int currJumpCopy = currJump;
    for(int i = 0; i< nums.length-1; i++){
    currJump = Math.max(currJump, nums[i]+i);
    if(i == currJumpCopy){
    res++;
    currJumpCopy = currJump;
    }
    }
    return res;
    }
    }

  • @maxbarahona9675
    @maxbarahona9675 5 лет назад

    Tushar is the God of Dynammic Programming.

  • @BadriNathJK
    @BadriNathJK 8 лет назад +1

    Much more easier solution:
    public int jump(int[] A) {
    int max=0;
    int e=0;
    int sc=0;
    for( int i=0;i

  • @arkadeep8049
    @arkadeep8049 5 лет назад +1

    You could have used Greedy Algorithm too! That would be a more efficient solution!

    • @charan775
      @charan775 5 лет назад +1

      greedy will give wrong solution.

  • @KirubaEbenezer
    @KirubaEbenezer 8 лет назад

    Hello Tushar I can't understand ur egg dropping problem....

  • @jayeshborgaonkar9166
    @jayeshborgaonkar9166 5 лет назад

    good solution using dp , thanks for the video

  • @mehulkumar8648
    @mehulkumar8648 4 года назад

    How u came to know that this problem is D.P problem..??

  • @FinanceStoryTime
    @FinanceStoryTime 8 лет назад +1

    As always, love Tushar's (the master dynamic programmer!) videos.

  • @aatifnazar1766
    @aatifnazar1766 5 лет назад

    There is a linear solution to this problem which uses a greedy approach

  • @midhunkraj7836
    @midhunkraj7836 4 года назад

    I dont get the table to equation portion. Can anyone help me?

  • @nikhilesh551
    @nikhilesh551 7 лет назад

    What if there are negative numbers in the array?

  • @natesh1
    @natesh1 4 года назад

    See Friendly Developer channel DP Deep Dive series . He explains each problem with all the required intuition and reasons that is very easy to understand.

  • @veerrajuyeleti8541
    @veerrajuyeleti8541 8 лет назад

    sir could you keep the solution to this program using graph approach

  • @chetandiwan
    @chetandiwan 5 лет назад

    Can you please solve this problem as well.
    From leetcode: Odd Even Jump

  • @RandomNagarsoge
    @RandomNagarsoge 3 года назад

    You need more explanation man on why to use DP n all.

  • @shwetapal8767
    @shwetapal8767 4 года назад

    sir you are the best

  • @rohitpal36
    @rohitpal36 4 года назад

    nice explanation

  • @yuvrajagarkar8942
    @yuvrajagarkar8942 2 года назад

    why is this so complex ???

  • @nishithakur424
    @nishithakur424 6 лет назад +2

    🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏
    waiting for ur new video ...

  • @AnkitChaudhary2601
    @AnkitChaudhary2601 8 лет назад

    @Tushar, This can be done in O(n).
    Here is the solution: github.com/AnkitChaudhary2601/ds/commit/6a9491768c848117ead70bcdc7c8b184c8009765

    • @Khogen
      @Khogen 8 лет назад

      Your without dp solution gives different result for the test case. n=6 arr= {3,3,4,1,5,2}. You should check this.

  • @shanmukhchandrayama8508
    @shanmukhchandrayama8508 3 года назад

    Can you please upload o(n) solution

  • @mujtabaarfat717
    @mujtabaarfat717 4 года назад

    This method will yeild TLE . best approach is Greedy Ladder approach.

  • @rohanarora7636
    @rohanarora7636 4 года назад +1

    isko kuch aata bhe h? Raat ke aaya hua lagta h ye

  • @rootedwithsami
    @rootedwithsami 7 лет назад +1

    Could be done in O(n), slow solution

  • @shubhamagrawal4997
    @shubhamagrawal4997 6 лет назад

    can we solve it in O(n)??

    • @rohitnarang007
      @rohitnarang007 6 лет назад

      yes...you can do that with greedy algo approach

  • @shivjyotigarai2141
    @shivjyotigarai2141 3 года назад

    'Yes we will use dynamic programming" 😎😎😎

  • @vibhavrathee6788
    @vibhavrathee6788 4 года назад

    Dude.. I guess this method isn't optimized.

  • @vrushankdoshi7813
    @vrushankdoshi7813 5 лет назад +1

    good approach but its showing as Time Limit Exceed

    • @mukeshgupta85
      @mukeshgupta85 5 лет назад

      Same here, by this explanation, on leed code showing Time Limit Exceed

  • @sharonbinto
    @sharonbinto 3 года назад

    Thank You

  • @samarthsharma1184
    @samarthsharma1184 5 лет назад

    This problem can be solved in O(n)..

  • @vsrivatsan4464
    @vsrivatsan4464 5 лет назад

    nothing clear

  • @ChaneLX645
    @ChaneLX645 Год назад

    my brain hurts

  • @SwikarP
    @SwikarP 7 лет назад

    fantastic. tahnks

  • @suramudaykiran6729
    @suramudaykiran6729 4 года назад

    U can solve it in o(n)

  • @spicytuna08
    @spicytuna08 6 лет назад

    thanks Tushar. this can done cleanly using 2D matrix.

  • @Bakepichai
    @Bakepichai 7 лет назад

    Thank you Tushar

  • @priyamvora9722
    @priyamvora9722 7 лет назад +2

    Need O(n) Solution..!!

  • @divyanshruhela
    @divyanshruhela 4 года назад

    I solved this question on leetcode with same lagic but it got TLE for only 2 test cases out of 95, I thought you will tell about any better approach can you??

  • @KunalShah62
    @KunalShah62 7 лет назад

    There is O(n) solution for this problem - BFS

  • @emilyw6762
    @emilyw6762 7 лет назад

    very helpful

  • @pareshvadhvani3457
    @pareshvadhvani3457 9 лет назад +2

    ***** This can be solved by greedy approach also.....

  • @rituagrawal2218
    @rituagrawal2218 7 лет назад

    U the best

  • @abhishekcpc5044
    @abhishekcpc5044 4 года назад

    Whiteboard expl very useful

  • @redscorpions7440
    @redscorpions7440 6 лет назад

    difficult to understand , too fast, he didnt explain how did he get array values 2, 3 1 1 ,,,,,,and also he uses 2 extra spaces (arays)

    • @codeonmars579
      @codeonmars579 5 лет назад

      Can check this video if it helps ruclips.net/video/WyIDphqyUCU/видео.html

  • @taruvarmittal1484
    @taruvarmittal1484 4 года назад

    THis approach gives TLE

  • @sanialdas5672
    @sanialdas5672 3 года назад +1

    sala ratta marke aaya hai

  • @dhruvbhanderi3407
    @dhruvbhanderi3407 3 года назад

    bade aaram se

  • @ujjvalpatel5353
    @ujjvalpatel5353 7 лет назад +2

    O(n) !!!!!!!!!!

  • @abhishekkamboj7150
    @abhishekkamboj7150 3 года назад

    Ur English so irritate aap engrejo vali English kyo bolre ho kya jaareurat hh iski normal bi to bol skte go

  • @gaurika4927
    @gaurika4927 3 года назад

    the guy doesn't has teaching acumen. poor video. wish Aditya would have made this video

  • @tanmayagarwal8513
    @tanmayagarwal8513 3 года назад

    Noob solution!! .. Give us the O(n) solution

  • @PulkitMalhotra
    @PulkitMalhotra 2 года назад

    Bhhhhhhh