Minimum jump to reach end

Sdílet
Vložit
  • čas přidán 27. 03. 2015
  • Given an array, find minimum number to jumps to reach end of array, given you can jump at max as much as value at position in array.
    github.com/mission-peace/inte...
    github.com/mission-peace/inte...

Komentáře • 156

  • @baviskarakshay
    @baviskarakshay Před 4 lety +119

    Captions Autogenerator: My name is too sharp😂😂

  • @niyousha6868
    @niyousha6868 Před 3 lety +5

    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.

  • @shikharbhatia595
    @shikharbhatia595 Před 9 lety +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! :)

  • @SoumyajitGanguly_ALKZ
    @SoumyajitGanguly_ALKZ Před 7 lety +59

    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 Před 6 lety +1

      Soumyajit Ganguly yes this is the more intuitive solution too.

    • @sachinchandwani4085
      @sachinchandwani4085 Před 4 lety +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 Před 4 lety

      @@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 Před 4 lety +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 Před rokem

      @@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]

  • @MrRichiban
    @MrRichiban Před 2 lety +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.

  • @TheTutorialspoint
    @TheTutorialspoint Před 9 lety +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

  • @SritharRamadoss
    @SritharRamadoss Před 6 lety

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

  • @Life2Experience
    @Life2Experience Před 4 lety

    Awesome explaination!!! Keep up the great work buddy

  • @OtakRajCodes
    @OtakRajCodes Před rokem +1

    Excellent explanation . absolutely loved the blackboard style teaching.

  • @lylescottiii3441
    @lylescottiii3441 Před 9 lety +1

    Thanks for your video, great stuff

  • @anirudhdevpura2798
    @anirudhdevpura2798 Před 3 lety +10

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

  • @arindam..9486
    @arindam..9486 Před 9 lety

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

  • @FinanceStoryTime
    @FinanceStoryTime Před 7 lety +1

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

  • @jiyanaadlakha
    @jiyanaadlakha Před 9 lety

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

  • @shashankdubey7461
    @shashankdubey7461 Před 4 lety

    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 .

  • @techieinformation4701
    @techieinformation4701 Před 9 lety +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.

  • @MrRys
    @MrRys Před 7 lety +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?

  • @sumeetvandakudri9784
    @sumeetvandakudri9784 Před 7 lety +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 Před 5 lety

      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 Před 3 lety +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 Před 3 lety +1

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

  • @nicolaschataing1457
    @nicolaschataing1457 Před 5 lety +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 Před 4 lety

      can u share the code pls!

    • @NitinPal101
      @NitinPal101 Před 3 lety

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

  • @priyanshiagarwal7032
    @priyanshiagarwal7032 Před 4 lety +40

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

    • @shivanshutiwari5646
      @shivanshutiwari5646 Před 3 lety +38

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

    • @harichandana9891
      @harichandana9891 Před 3 lety +2

      Exactly 😭

    • @amey203
      @amey203 Před 3 lety +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 Před 3 lety +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 Před 3 lety +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.

  • @PriyankaPatel-gx3pj
    @PriyankaPatel-gx3pj Před 3 lety

    You always makes dp easy for us

  • @divyanshruhela
    @divyanshruhela Před 3 lety +1

    bhai tu GOD hai Massive Respect

  • @lylescottiii3441
    @lylescottiii3441 Před 9 lety

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

  • @genki316
    @genki316 Před 5 lety

    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.

  • @singvijaya
    @singvijaya Před 8 lety

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

  • @sharathnagendra3318
    @sharathnagendra3318 Před 2 lety +2

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

  • @ajinkyakale8941
    @ajinkyakale8941 Před 9 lety

    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)?

  • @ekaanshkhosla
    @ekaanshkhosla Před 7 lety +5

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

  • @tsaideepak8762
    @tsaideepak8762 Před 5 lety +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)

  • @mohammedrafeeq4902
    @mohammedrafeeq4902 Před 6 lety

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

  • @nishithakur424
    @nishithakur424 Před 6 lety +2

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

  • @nldanand
    @nldanand Před 5 lety +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 Před 3 lety

      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 Před 3 lety

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

  • @saketbhojane5697
    @saketbhojane5697 Před 7 lety

    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.

  • @shivamtaparia9948
    @shivamtaparia9948 Před 4 lety

    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

  • @chintamadhu001
    @chintamadhu001 Před 9 lety

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

  • @avinashkhetri9575
    @avinashkhetri9575 Před 4 lety +7

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

  • @jayeshborgaonkar9166
    @jayeshborgaonkar9166 Před 4 lety

    good solution using dp , thanks for the video

  • @shivanshutiwari5646
    @shivanshutiwari5646 Před 3 lety +2

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

  • @KanagaveluSugumar
    @KanagaveluSugumar Před 8 lety +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 Před 8 lety

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

  • @anonymousvoid6356
    @anonymousvoid6356 Před 2 lety +1

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

  • @maxbarahona9675
    @maxbarahona9675 Před 4 lety

    Tushar is the God of Dynammic Programming.

  • @aritrakumarbara5994
    @aritrakumarbara5994 Před 3 lety

    Very well explanation sir🙏

  • @ayushjindal4981
    @ayushjindal4981 Před 4 lety +4

    Best solution is in O(N).

  • @arvindkumar95
    @arvindkumar95 Před 8 lety

    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.

  • @vasy4321
    @vasy4321 Před 8 lety +3

    - problem desciption -
    So how do we solve this?

    • @HarishAmarnath
      @HarishAmarnath Před 4 lety

      yes, we will use dynamic programming to solve this

  • @mayankagarwal7016
    @mayankagarwal7016 Před 7 lety +2

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

  • @arkadeep8049
    @arkadeep8049 Před 5 lety +1

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

    • @charan775
      @charan775 Před 4 lety +1

      greedy will give wrong solution.

  • @SwikarP
    @SwikarP Před 6 lety

    fantastic. tahnks

  • @pnachtwey
    @pnachtwey Před 7 lety

    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.

  • @rohitpal36
    @rohitpal36 Před 3 lety

    nice explanation

  • @sujoyseal195
    @sujoyseal195 Před 3 lety

    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.

  • @shwetapal8767
    @shwetapal8767 Před 3 lety

    sir you are the best

  • @shivprakashsharma9511
    @shivprakashsharma9511 Před 7 lety +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

  • @emilyw6762
    @emilyw6762 Před 6 lety

    very helpful

  • @spicytuna08
    @spicytuna08 Před 5 lety

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

  • @veerrajuyeleti8541
    @veerrajuyeleti8541 Před 7 lety

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

  • @chetandiwan
    @chetandiwan Před 4 lety

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

  • @shrimpo6416
    @shrimpo6416 Před 2 lety

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

    • @sranil
      @sranil Před rokem +1

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

  • @Bakepichai
    @Bakepichai Před 7 lety

    Thank you Tushar

  • @shivjyotigarai2141
    @shivjyotigarai2141 Před 3 lety

    'Yes we will use dynamic programming" 😎😎😎

  • @rituagrawal2218
    @rituagrawal2218 Před 7 lety

    U the best

  • @BadriNathJK
    @BadriNathJK Před 7 lety +1

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

  • @KirubaEbenezer
    @KirubaEbenezer Před 8 lety

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

  • @arnavkarforma3015
    @arnavkarforma3015 Před 3 lety

    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;
    }
    }

  • @imyounick
    @imyounick Před 2 lety

    What if we have -ve number ?

  • @RandomNagarsoge
    @RandomNagarsoge Před 2 lety

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

  • @midhunkraj7836
    @midhunkraj7836 Před 4 lety

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

  • @mujtabaarfat717
    @mujtabaarfat717 Před 3 lety

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

  • @sharonbinto
    @sharonbinto Před 2 lety

    Thank You

  • @nikhilesh551
    @nikhilesh551 Před 7 lety

    What if there are negative numbers in the array?

  • @mehulkumar8648
    @mehulkumar8648 Před 3 lety

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

  • @natesh1
    @natesh1 Před 4 lety

    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.

  • @ChaneLX645
    @ChaneLX645 Před 9 měsíci

    my brain hurts

  • @aatifnazar1766
    @aatifnazar1766 Před 4 lety

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

  • @Star_Bawa9
    @Star_Bawa9 Před 2 lety +2

    I kindof understood nothing ...

  • @shanmukhchandrayama8508

    Can you please upload o(n) solution

  • @shubhamagrawal4997
    @shubhamagrawal4997 Před 6 lety

    can we solve it in O(n)??

    • @rohitnarang007
      @rohitnarang007 Před 6 lety

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

  • @AnkitChaudhary2601
    @AnkitChaudhary2601 Před 8 lety

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

    • @Khogen
      @Khogen Před 8 lety

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

  • @abhishekcpc5044
    @abhishekcpc5044 Před 4 lety

    Whiteboard expl very useful

  • @KunalShah62
    @KunalShah62 Před 7 lety

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

  • @rootedwithsami
    @rootedwithsami Před 7 lety +1

    Could be done in O(n), slow solution

  • @yuvrajagarkar8942
    @yuvrajagarkar8942 Před rokem

    why is this so complex ???

  • @samarthsharma1184
    @samarthsharma1184 Před 5 lety

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

  • @suramudaykiran6729
    @suramudaykiran6729 Před 3 lety

    U can solve it in o(n)

  • @priyamvora9722
    @priyamvora9722 Před 7 lety +2

    Need O(n) Solution..!!

  • @rohanarora7636
    @rohanarora7636 Před 3 lety +1

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

  • @vsrivatsan4464
    @vsrivatsan4464 Před 5 lety

    nothing clear

  • @ujjvalpatel5353
    @ujjvalpatel5353 Před 7 lety +2

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

  • @taruvarmittal1484
    @taruvarmittal1484 Před 3 lety

    THis approach gives TLE

  • @pareshvadhvani3457
    @pareshvadhvani3457 Před 9 lety +2

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

  • @dhruvbhanderi3407
    @dhruvbhanderi3407 Před 2 lety

    bade aaram se

  • @PulkitMalhotra
    @PulkitMalhotra Před 2 lety

    Bhhhhhhh

  • @divyanshruhela
    @divyanshruhela Před 3 lety

    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??

  • @sanialdas5672
    @sanialdas5672 Před 2 lety +1

    sala ratta marke aaya hai

  • @vibhavrathee6788
    @vibhavrathee6788 Před 3 lety

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

  • @vrushankdoshi7813
    @vrushankdoshi7813 Před 4 lety +1

    good approach but its showing as Time Limit Exceed

    • @mukeshgupta85
      @mukeshgupta85 Před 4 lety

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

  • @tanmayagarwal8513
    @tanmayagarwal8513 Před 3 lety

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

  • @redscorpions7440
    @redscorpions7440 Před 5 lety

    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 Před 5 lety

      Can check this video if it helps czcams.com/video/WyIDphqyUCU/video.html

  • @gaurika4927
    @gaurika4927 Před 2 lety

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

  • @abhishekkamboj7150
    @abhishekkamboj7150 Před 2 lety

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