🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤
@dineshkumarkb1372 Жыл бұрын
All your videos are a treasure . Every single one is worth rewatching during interviews. Never ever delete these videos or stop uploading new ones.
@naive-fleek742011 ай бұрын
during interviews??
@dineshkumarkb137211 ай бұрын
@@naive-fleek7420 I meant during prep for interviews. It’s implied dude.
@venkateshnaidu21332 жыл бұрын
Amazing solution, loved it! Here is a minor tweak to handle an edge case (no possible path) int minJumps(int arr[], int n){ // Your code here int l=0, r=0; int jumps = 0; while(r < n-1) { int farthest = 0; for(int i = l; i r) return -1; jumps++; } return jumps; }
@akshatsamdani Жыл бұрын
Thanks for posting this. I was thinking about the same
@PorkBoy697 ай бұрын
The test cases are generated such that you can reach nums[n - 1].
@PRAVEENKUMAR-fi1wu4 ай бұрын
No need to do this. It is said in question. "We can always reach to the end" @@akshatsamdani
@aniketyadav6247 Жыл бұрын
The below code also works : 1.) Traverse the entire nums array. On each i-th iteration, update the farthest_jump to the max of current value of farthest_jump and i + nums[i] 2.) If i is equal to the current jump we have completed the current jump and can now prepare to take the next jump (if required). So we increment the jump by 1 and set curr_jump to farthest jump. 3.) If that's not the case then do not update the jumps variable and the curr_jump variable since we haven't yet completed the current jump. 4.) In the end of the traversal you will get the minimum jumps. Hope this helps :) def jump(self, nums: List[int]) -> int: farthest_jump = 0 jump = 0 curr_jump = 0 for i in range(len(nums)-1): # Find the Farthest Jump farthest_jump = max(farthest_jump, i + nums[i]) # it means we have made the jump if i == curr_jump: # Point curr jump to the farthest jump curr_jump = farthest_jump jump += 1 return jump
@allen7243 жыл бұрын
This is a great explanation. I like this method better than LeetCode's published solution! It is more intuitive for me. Thanks and keep up these videos.
@lakshmanprabhu6722 Жыл бұрын
Same here!. Went through a lot of solutions, but this is just gold.
@ghdshds18992 ай бұрын
this explanation was actually dog
@matthewtang14903 жыл бұрын
Please don't stop making videos :) I just binged your DP playlist in 2 days
@NeetCode3 жыл бұрын
Wow, I bet you would nail any interview now! Thanks for the kind words
@crimsonghoul89837 ай бұрын
2 DAYS????
@lil_jekeАй бұрын
50 vidoes In 2 days?
@acala1273 жыл бұрын
This is the most elegant and clear explanation I have seen for this problem. Thank you!
@prashantgupta61603 жыл бұрын
please continue this series, you are born to teach coding to other people.
@karthikinamanamelluri7208 Жыл бұрын
Great explanation!! Even after this I was confused why the while condition is r < len(nums)-1, and not r < len(nums). I thought why can't we can change it to r < len(nums), and return res-1. This explains the algorithm better, since the result we are finding from the loop is the no. of blocks, and the no. of jumps is one less than no. of blocks. But this solution is not working for all the cases.
@licokr Жыл бұрын
Crazy. This channel explains coding solutions in the easiest way. It saves my life.
@ng.manisha11 ай бұрын
You are literally a savior! I have my google interview lined up soon and all your videos actually teach me tricks how to think when faced with a problem!
@iamnoob75933 ай бұрын
did u make it into Google?
@heroicrhythms8302 Жыл бұрын
if we come to the middle of the array and can't reach the end anymore, that means we have encountered a 'zero'. then we should return -1. implement a check saying (if l>r: return -1)
@ma_sundermeyer2 жыл бұрын
Nice simplification of the BFS.. I had a similar idea but stayed with the conventional deque implementation: q = deque([(0,0)]) max_i = 1 while q: i,num_j = q.popleft() if i >= len(nums) - 1: return num_j for j in range(max_i, nums[i] + i + 1): q.append((j, num_j+1)) max_i = max(max_i, nums[i] + i + 1)
@ssvivek410 ай бұрын
I don't think if I'll ever see a better explanation to this problem. Kudos man!
@ekanshsharma1309 Жыл бұрын
why we write r < nums.size() - 1..... not just r< nums.size()??
@RiteshThakur-o6r4 ай бұрын
Because if r is at nums.size, then it has to terminate because it has reached the final index.
@pranavmaiya43862 ай бұрын
hey did you get placed?
@protyaybanerjee50513 жыл бұрын
Man, we need more videos. Great production quality :)
@soumyajitganguly25932 ай бұрын
Great explanation man. Easy to generalize when jumping to last value is not possible.
@denshaSai3 жыл бұрын
whats the reason that it is "while r < len(nums) - 1:" not just "while r < len(nums) :"?
@arunavbhattacharya33533 жыл бұрын
I fail to intuitively understand why do we need to iterate till n-1 instead of n
@eternalmeme79202 жыл бұрын
@@arunavbhattacharya3353 It's because, 1) All no. are positive, therefore if we touch the n-2 element, i.e. r=n-2, then we are iterating from l to r(inclusive), if we iterate at r=n-2, then since all no. positive, farthest will definitely be greater than >=n-1, therefore r
@visase20362 жыл бұрын
One of the main reasons for this is , Since we are accounting the 1st jump at position 0 , we are not considering the last element to calculate the answer ie [2,3,1,1,4] We re incrementing ans+1 by taking 2 into account , which is not necessary if you work logically and since we are accounting that as one jump we are ignoring the last element!
@pruthvirajpatil7798 Жыл бұрын
Simple - Because if r is at len(nums)-1, we would have achieved the goal. No need to proceed ahead.
@adityagoyal42998 ай бұрын
The content of this channel is priceless. Been binge watching your videos
@sagarnair90212 жыл бұрын
one line should be added after updating j i.e if j
@Viso-code471Ай бұрын
for c++ -> int res = 0; int l = 0 , r = 0; int farthest = 0; while(r
@arunraj25272 жыл бұрын
I love his patience and way of talking through the problem.
@darkexodus6404 Жыл бұрын
Your explanation is too good! Understood it clearly.
@yathartharana7956 Жыл бұрын
Searching the whole day and find this solution the best one 🙌🏼
@divyanshkhetan Жыл бұрын
This is a great approach. I especially liked how you related it to the concept of BFS. It helped in visualizing the approach so much!
@VarunMittal-viralmutant2 жыл бұрын
The standard solution for this question is like the LIS variant which is O(N**2). And that gives TLE on LeetCode I think the solution described above works only when it is guaranteed that the end can be reached, else it fails. Correct ? Modified to take care of unreachable cases: def find_shortest_jump_path(jumps): l, r = 0, 0 i = 0 res = 0 while l = len(jumps)-1 else -1
@God0fSloth9 ай бұрын
Thank you very much! Your code solved my problem. But what's the variable ( i ) used for? why are we increasing it?
@VarunMittal-viralmutant9 ай бұрын
@@God0fSloth That looks like some junk code, not used in final solution :)
@anmolgupta945Ай бұрын
Also, isn’t it also 0(N^2) ? For loop inside while loop ?
@megavoltism2 жыл бұрын
It's funny how he always colors-in his arrow heads Anyway, really cool insight about BFS!
@chandrachurmukherjeejucse58169 ай бұрын
Your explanation and drawing is just awesome.
@loia5tqd0014 ай бұрын
9:28 Why while r < len(nums) - 1 not while r < len(nums) ?
@abibhavankumar25223 ай бұрын
The array positions starts from 0 to length of array - 1. If you go till len(nums) then you will be considering an extra element that is not present in the array. Hope that clears your doubt!
@meghaldarji5983 жыл бұрын
The best solution there is for this problem. I am saying after watching all other videos on this problem.🙌🙌
@ge_song58 ай бұрын
question: why do we have to stop by the last_index - 1? while r < len(nums) - 1:
@venkatrampramod79782 жыл бұрын
Your solutions are absolutely brilliant. I love the way you break down the solution with diagrams.
@charan7752 жыл бұрын
This has got to be the best intuitive explanation out there for this problem
@sniper3243 жыл бұрын
Your videos and explanations are some of the best on KZbin, really great stuff man, keep it up!
@themagickalmagickman Жыл бұрын
I used a Dijkstra's approach to solve the problem, but this is a simpler and quicker answer... wow.
@ahmad38233 жыл бұрын
I am new to serious coding but great job! this took me some time and now way close to this neatness level!
@akshatgupta1073 жыл бұрын
Please please man, I love your channel so much, you have never disappointed me. Make a list of important greedy problems please
@krishnavamsichinnapareddy2 жыл бұрын
You simply nailed it. Love from India ♥️
@sauravchandra102 ай бұрын
Loved the analogy to BFS.
@PrathamMittal-t8m Жыл бұрын
Best channel for explaining the leetcode problems to a dumbo like me
@MrExamer2 жыл бұрын
great explanation on the BFS mind behind the problem
@kevintran61022 жыл бұрын
This explanation is crystal clear. Thank you!
@fahimislam968610 күн бұрын
can anyone tell me why r < len(nums)-1; cant it be r
@Gnaneshh3 жыл бұрын
One hell of an explanation ! Thank you
@kashishsharma68093 жыл бұрын
Oh man I unnecessarily used queue like in bfs. 🤧 I implemented exact bfs to find hops.. But using the interval instead of queue was awesome 😎
@stith_pragya Жыл бұрын
Thank You So Much for this wonderful video................🙏🙏🙏🙏🙏🙏
@bahaaiman85883 жыл бұрын
This is the only one that I understood. Thanks a ton !
@NeetCode3 жыл бұрын
Happy to help!
@rohanb95122 жыл бұрын
Nicely explained the intuition. Exactly wat i was looing for. Probably the best explanation in YT
@user-pn9sr2rq3z8 ай бұрын
great explanation and video! Espcially coloring. Unfortuntatelly does not work on leetcode any longer , giving timeout exceeded error.
@shashijais7892 жыл бұрын
Again Superb solution, which I was looking for! Thanks for this explanation.
@aravinda1595 Жыл бұрын
You are so good I just need to watch the explanation part and boom ! i can write my own code
@kewtomrao2 жыл бұрын
Awesome explanation!! I did the regualr BFS and got stuck in a MLE error. Now i know my mistakes.
@DivyaSingh-bl4cj2 жыл бұрын
Best explanation channels for python.
@vincenttung-or3ns2 ай бұрын
This is freaking genius!
@supervince1102 жыл бұрын
You always have the simplest solution!
@huatsai433 жыл бұрын
Thank you for the clear drawing explanation!
@NeetCode3 жыл бұрын
Thanks, happy it was helpful 🙂
@gokulnaathbaskar98082 жыл бұрын
Nice, looking at BFS for the first time in an array.
@ankurtiwari56642 жыл бұрын
i don't know python still i watch all ur videos Next level explanation of every approach
@dent18082 жыл бұрын
clearest explanation ever
@697Alok3 жыл бұрын
What an explanation. Loved it :)
@venkatsaireddy141211 ай бұрын
why left is re-initialised with only right +1, it can be moved to more than right +1?
@thevagabond85yt Жыл бұрын
I programmed by modifying Greedy Approach of Jump Game(I) : ``` class Solution { public int jump(int[] nums) { int N= nums.length, goal= N-1, jumpCount= 0; while(goal >0) { for(int i=0; i= goal) { goal= i; jumpCount++; break; } // if }//for }//while return jumpCount; } } ```
@shantanukumar40812 жыл бұрын
Great Explanation !!!
@ardhidattatreyavarma53379 ай бұрын
what a beautiful piece of code
@Cld1363 жыл бұрын
Excellent explanation. Much Thanks!
@francescosacripante1661 Жыл бұрын
Correct me if I'm wrong, but couldn't we solve it by using Dijkstra algorithm? I mean we could create a graph and with this search for the shortest path to the end
@shantanushende63 жыл бұрын
really really good! Felt like one comment did not do justice to the level of simplicity!
@vishalmishra70183 жыл бұрын
Really neat code! Nicely done and explained.
@Kushagra_213 жыл бұрын
one of the best solution in internet for this question. Thanks a lot!!
@user-2802cvsfkj Жыл бұрын
this is nuts dawg, who allowed this to exist. im literally shaking and crying rn, wtf.
@rabbyhossain6150 Жыл бұрын
Memory Complexity: O(n) class Solution: def jump(self, nums: List[int]) -> int: steps = 0 visited, q = set(), collections.deque() q.append(0) visited.add(0) while q: length = len(q) for _ in range(length): idx = q.popleft() if idx == len(nums) - 1: return steps for pos in range(1, nums[idx] + 1): if (idx + pos) not in visited: q.append(idx + pos) visited.add(idx + pos) steps += 1
@xinniu31452 жыл бұрын
Thank you for sharing. We can put farthest=0 before the while loop right?
@premthapa9959 Жыл бұрын
i cant fathom how easily u did smthing i took hours and stil couldnt crack
@trantung20133 жыл бұрын
Really elegant solution.
@avishekarora2 жыл бұрын
Best Solution explaination, thanks
@amanshaukat11052 жыл бұрын
This is a great explanation. Very intuative. lets say it is not guaranteed that answer will exist, and we have to return -1 in such case. How could we handle this. Please help.
@Kellykmixtape7 ай бұрын
bro is a genius
@amarrajeev29032 жыл бұрын
I think the edge cases also must be handled in the code. Suppose if the Algorithm could not find the minJumps it should return -1 as such. Thoughts on this?
@markolainovic Жыл бұрын
No need, in the description it says that the solution existence is guaranteed.
@allwell857011 ай бұрын
You simplified it!! Thanks
@bibibaba2505 Жыл бұрын
best answer I have ever seen.
@jhanvisaraswat6976 Жыл бұрын
wow, BFS. Never thought it could be done like that
@vasumahalingam516210 ай бұрын
brilliant but I wouldnt be able to solve this by myself in a coding interview. Very clever indeed.
@gulershad43752 жыл бұрын
def jump(nums): jump = 0 farthest = float('-inf') end = 0 for i in range(len(nums)): farthest = max(farthest, nums[i]) + 1 if i == end and i != len(nums) - 1: jump += 1 end = farthest return jump
@tobito__3 жыл бұрын
How is this solution a O(n)? Isnt there a for loop inside a while loop which makes it a O(n^2)?
@abhinav-lq9ms3 ай бұрын
I think it's O(n) since we are always updating l to be r+1, so it won't again traverse the same value
@anupamdubey57362 жыл бұрын
Best Explanation! Thanks
@rohands91953 ай бұрын
Why is the time complexity of DP step = O(n^2)?
@appi1472 жыл бұрын
amazing explanation
@SMAsaduzzamanAsad8803 жыл бұрын
Very good explanation!
@TheLaidia3 жыл бұрын
very clear explanation. Thank you!!
@yashjain1011 Жыл бұрын
such a nice explanation. This video was so great. You earned a subscriber.
@LeCharles-bw4wp11 ай бұрын
Thanks.But I suppose the time should be O(n squares)
@amrutaparab49397 ай бұрын
Thank god for you!
@alonalon87942 жыл бұрын
at the end of the algorithm r might point to an index which is lesser than the last index?
@starkhunt26842 жыл бұрын
Great explaination mate
@subhajitrakshit98662 жыл бұрын
Love this solution...Thanks
@arihantjain32742 жыл бұрын
Best explanation....
@dennisg36838 ай бұрын
Why does the first time iterating through the array the for loop starts at 0 and goes to just the first index (0 + 1)?
@commandernorton882 жыл бұрын
Drinking game: take a sip when he says "Right?"
@darshansimha21662 жыл бұрын
Nice video, I understand the solution. But I am having a tough time understanding the complexity for an array of all 1's wont the for loop inside run for every iteration in the array, so won't that make it O(n^2)?
@divyasri94322 жыл бұрын
I have got the same doubt forloop inside while loop will give time complexity O(n^2) how does this become optimal solution?
@bmusuko2 жыл бұрын
@@divyasri9432 I think the key here, we shift the left pointer to r+1 so we only inspect each element in the array once, it means it is o(n)
@minhthinhhuynhle91032 жыл бұрын
2 loops doesn't mean O(N^2)
@sunshine-mc2oi2 жыл бұрын
Thank you for the awesome video. It's super easy to understand. Is there any chance you can make a video about 1326. Minimum Number of Taps to Open to Water a Garden and Video Stitching, they seem relevant to this topic. Thank you so much.