Jump Game - Greedy - Leetcode 55

Ғылым және технология

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
Coding Solutions: • Coding Interview Solut...
Dynamic Programming Playlist: • House Robber - Leetco...
Tree Playlist: • Invert Binary Tree - D...
Linked List Playlist: • Reverse Linked List - ...
Problem Link: neetcode.io/problems/jump-game
0:00 - Read the problem
2:50 - Brute Force Explanation
10:10 - Greedy Explanation
14:05 - Coding Explanation
leetcode 55
This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
#greedy #array #python

Пікірлер: 274

  • @davidbanda4727
    @davidbanda47273 жыл бұрын

    Excellent explanation, you explain the algorithms so easy and well. Observation: You can write the last line of code as: return goal == 0. regards!

  • @pankajjatav6448

    @pankajjatav6448

    Жыл бұрын

    Also you can start loop from second last element: for i in range(len(nums)-2, -1, -1):

  • @yonavoss-andreae4952

    @yonavoss-andreae4952

    10 ай бұрын

    @@pankajjatav6448 that triggers an edge case in which two elements returns false ie [1,2]

  • @gordonwu8117

    @gordonwu8117

    7 ай бұрын

    @@yonavoss-andreae4952 if length is 0 then "i" will start on index 0, which works as intended

  • @learnwithujjwal_

    @learnwithujjwal_

    4 ай бұрын

    or simply return not goal

  • @hash00ify
    @hash00ify2 жыл бұрын

    we can start the loop from the len(nums) - 2 position since goal is already set at the last position when we declare it.

  • @shashanksrivastava7262

    @shashanksrivastava7262

    Жыл бұрын

    I am acturally surprised how his code actually worked like, wouldn't i+nums[i] be always greater than goal ?

  • @anupamkolay193

    @anupamkolay193

    Жыл бұрын

    @@shashanksrivastava7262 Yes I'm also thinking about that too.

  • @quanghuyluong1249

    @quanghuyluong1249

    Жыл бұрын

    @@shashanksrivastava7262 Consider [3,2,1,0,4]: i+nums[i] would not be greater and the goal will never be shifted

  • @experiment8924

    @experiment8924

    11 ай бұрын

    @@shashanksrivastava7262 Yes, i+nums[i] would be greater than goal for the first iteration, which would satisfy the if statement but the goal will be the same, i.e the last index and the program would move on the next iteration and work as usual.

  • @shravankumar3717

    @shravankumar3717

    10 ай бұрын

    Thats why in this example we cannot reach last element. Algorithm works

  • @gunishjain9307
    @gunishjain9307 Жыл бұрын

    12:00 jaw dropping intuition, I could have never thought about it. Thanks for the explanation.

  • @pritampawar6478
    @pritampawar64782 жыл бұрын

    that explanation for greedy part was just awesome🔥🔥

  • @surajsharma2086
    @surajsharma20862 жыл бұрын

    Man excellent job. I went through many videos but wasn't able to understand. U made us all feel that it is very simple. Thanks a-lot again.

  • @gompro
    @gompro3 жыл бұрын

    This channel is so much underrated. This video is just amazing!

  • @ZQutui
    @ZQutui3 жыл бұрын

    Thank you for these videos, I found you recently and you are the channel I have been looking for

  • @heethjain21
    @heethjain212 жыл бұрын

    That greedy solution is ingenius. I am in awe!! Thank you.

  • @andrepinto7895
    @andrepinto78952 жыл бұрын

    There is no need to start from the end and move backwards. You can naturally progress from the beginning, like this: int reach = 0; for (int i = 0; i < nums.length && i = nums.length-1;

  • @ma_sundermeyer

    @ma_sundermeyer

    Жыл бұрын

    yeah, also started from the beginning, it's more intuitive. You can also stop early at zeros that you can't cross: max_index = -1 for i in range(len(nums)-1): if nums[i] == 0 and max_index

  • @PippyPappyPatterson

    @PippyPappyPatterson

    Жыл бұрын

    @@ma_sundermeyer Why does everyone start from the end? Does it help with other problems that use a similar solution (that can't be implemented front-forwards)? From the beginning is a million times easier to remember.

  • @eku333

    @eku333

    Жыл бұрын

    @@PippyPappyPatterson I disagree. NeetCode's solution is easier to understand imo.

  • @PippyPappyPatterson

    @PippyPappyPatterson

    Жыл бұрын

    @@eku333 do u normally iterate across ur arrays in reverse? or forwards?

  • @eku333

    @eku333

    Жыл бұрын

    @@PippyPappyPatterson iterating over an array in reverse is not a rarely used dp technique.

  • @suhailf5014
    @suhailf50142 жыл бұрын

    These are the questions/solutions that make me fall in love with algorithms :) many many thanks @NeetCode

  • @kalintoshev
    @kalintoshev2 жыл бұрын

    The tricky part of the greedy is to prove that it actually works; we clearly have multiple options for moving the goal and we always pick the first one. How do we guarantee that we are not going to get stuck using this approach?

  • @sivaprakashkkumar9691

    @sivaprakashkkumar9691

    2 жыл бұрын

    how it becomes greedy solution please explain it.

  • @mangalegends

    @mangalegends

    2 жыл бұрын

    This is what I have a little trouble wrapping my brain around. We always pick the first option, but what if the first option is 0? Then we're stuck lol. I guess you could add extra logic to handle that but the greedy solution doesn't seem to have include that

  • @VipulDessaiBadGAMERbaD

    @VipulDessaiBadGAMERbaD

    2 жыл бұрын

    but this prob is not to find optimal path but to find only if we can reach the destination, that is why its okay to select the first elment.

  • @Sim0000n

    @Sim0000n

    Жыл бұрын

    @@mangalegends Let's consider the array 3105. Goal is 3, i is 3. I becomes 2, goal stays the same (as it is 0.) I becomes 1, Goal stays the same as 1+1 is not greater or equal than 3. i becomes 0, but as 0+3 ≥ 3, we're good. So no reason to be stucked

  • @jorgemejia1586

    @jorgemejia1586

    Жыл бұрын

    Even if you hit a 0 during the loop, that’s fine. The goal post will be left at an actual reachable/moveable location (aka an index where the value is not zero)

  • @briangurka8085
    @briangurka80852 жыл бұрын

    greedy approach is brilliant. love it

  • @aninditaghosh6167
    @aninditaghosh6167 Жыл бұрын

    Outstanding illustration with the diagrams. I just tried out the same code looping i from len(nums) - 2 to -1 because anyway the first run didn't alter the goal's position and it got accepted :) thank you so much!

  • @KCML036
    @KCML0362 жыл бұрын

    amazing explanation. It really showcase why greedy approach can work wonders sometimes

  • @yuzhenwang8103
    @yuzhenwang81037 ай бұрын

    Great Explanation! Thx for making it clearer for greedy algorithm

  • @aryehakierman6388
    @aryehakierman6388 Жыл бұрын

    thats an awesome solution and explination!! I thought of an answer where we only stop our iteration once we come upon a zero. then we just check if we can if the zero can be jumped over by previous elements. the only problem is the edge case where nums= [0],where you got check for it. var jump = function (nums) { if (nums.length === 1) return true; let prevPosition, prevValue; let passFlag = true; for (let i = 0; i if (nums[i] === 0) { passFlag = false; prevPosition = i - 1; while (prevPosition >= 0) { prevValue = nums[prevPosition]; if (prevPosition + prevValue > i) { passFlag = true; break; } prevPosition--; } if (!passFlag) return false; } } return true; };

  • @cwash08
    @cwash082 жыл бұрын

    Nice. I practiced the dp solution and got stuck on greedy. I was able to make the connection to the last and second before last elements , but couldn’t think of moving the goalpost as you say. Nice solution.

  • @josh1234567892
    @josh1234567892 Жыл бұрын

    Love when I can implement someone's explanation without directly looking at the implementation. Thank you so much brotha

  • @NeetCode

    @NeetCode

    Жыл бұрын

    Nice! 💪

  • @guneskedi
    @guneskedi3 ай бұрын

    Oh my god I cannot believe I can finally understand, design the steps and write the correct code! Finally my work paid off!!

  • @gargichaurasia4103
    @gargichaurasia41033 жыл бұрын

    Your explanation works like magic! Thank you so much 😊

  • @abhinav3325
    @abhinav3325 Жыл бұрын

    Thank you so much sir! Your logics and way of explaining is really impressive and make attention to the solution.

  • @Getentertainedp
    @Getentertainedp Жыл бұрын

    your explanations are really easy to understand. I always look for your videos. I was looking for buy and sell stocks III and IV videos from your channel but did'nt find them. Watched other channel videos but they were not as easy to understand.

  • @phlix1
    @phlix15 ай бұрын

    I love how simple the solution is. I was sketching out a very complex one :D

  • @sscapture
    @sscapture4 күн бұрын

    Spent couple of hours with this task :( after watching first 3 minutes of the video, was able to write the solution. You're God!

  • @MrM2JT
    @MrM2JT Жыл бұрын

    The greedy approach is simply mind-blowing!

  • @gargibiswas8619
    @gargibiswas86192 жыл бұрын

    This is the best greedy solution I have seen till now!

  • @auroshisray9140
    @auroshisray91402 жыл бұрын

    Awesome solution man! Loved it

  • @thomaslee3621
    @thomaslee36213 жыл бұрын

    You and Tech Dose are my go to for leetcodes.

  • @NeetCode

    @NeetCode

    3 жыл бұрын

    Yeah, Tech Dose is really good at explaining.

  • @MrYp-ds7sz

    @MrYp-ds7sz

    3 жыл бұрын

    I found neetcode more clear and easy.

  • @rajdeepchakraborty7961

    @rajdeepchakraborty7961

    3 жыл бұрын

    @@MrYp-ds7sz neetcode>techdose

  • @adithyagowda4642

    @adithyagowda4642

    2 жыл бұрын

    @@rajdeepchakraborty7961 neetcode==techdose

  • @golammuhaimeen2825
    @golammuhaimeen28253 жыл бұрын

    really nicely explained man! thank you so much

  • @user-lq2us7xn6e
    @user-lq2us7xn6e2 жыл бұрын

    Thanks. You're very talented in explanation.

  • @mister_bake2743
    @mister_bake274310 ай бұрын

    Thanks mate, helped me a lot. Easy and very quick

  • @haroldobasi2545
    @haroldobasi25452 жыл бұрын

    Never stop making these please lol

  • @msteja
    @msteja2 жыл бұрын

    Explanation 💯 clean !

  • @ogundimuhezekiah845
    @ogundimuhezekiah845 Жыл бұрын

    Thanks buddy. You're literally the best

  • @youssifgamal8545
    @youssifgamal8545 Жыл бұрын

    I think u can also solve it in O(nlogn) using a BIT or segment tree , where u will start from the end and see whether the current node range (l = i , r = i+nums[i]) summation is greater than 0 or r >= nums.size() , if so then update the current node to be one and continue

  • @petergriffin422
    @petergriffin42210 ай бұрын

    excellent video. I am learning a lot from your videos. Great work

  • @baetz2
    @baetz2 Жыл бұрын

    You can also solve this problem by finding zeroes in the array and check if there are numbers before zeroes long enough to jump over zero. For example, if you see [3,2,1,0,...], you can instantly tell you're stuck at 0.

  • @tunepa4418

    @tunepa4418

    Жыл бұрын

    that makes sense but this is still technically O(n^2) right?. consider this example [2,3,4,5,6,7,8,8,0,0,0,0,0,0,0,0,0,0,9]. For every non zero element, we have to do ~n/2 work to check if it crosses over all the zeros (i.e constant amount of work for every non zero element)

  • @archiecalder5252

    @archiecalder5252

    11 ай бұрын

    Working backwards, if we record the index of the first encountered 0, then the work required to check if an element crosses is constant. @@tunepa4418

  • @jffrysith4365

    @jffrysith4365

    10 ай бұрын

    @@tunepa4418 no, I think it would be the same way, except we just only start checking once we find a zero. I don't think it'd be any faster though... because you'd still have to check if each value is a zero...

  • @kavabanger88

    @kavabanger88

    10 ай бұрын

    ​@@tunepa4418if we are going from the end of list and found a zero, and then found a long enough jump to jump over it, we dont care about zeroes between first zero and jump position

  • @856dtrad9

    @856dtrad9

    8 ай бұрын

    @@tunepa4418 O(n) class Solution: def canJump(self, nums: List[int]) -> bool: obstacle = -1 for pos in range(len(nums) - 2, -1, -1): if nums[pos] == 0 and obstacle == -1: obstacle = pos if pos + nums[pos] > obstacle: obstacle = -1 return obstacle == -1

  • @laumatthew71
    @laumatthew71 Жыл бұрын

    awesome solution and explanation, thank you !

  • @wenqingjiang8607
    @wenqingjiang86073 жыл бұрын

    Excellent explanation and great video! Thank you!

  • @NeetCode

    @NeetCode

    3 жыл бұрын

    Glad it was helpful!

  • @emilyplanes7082
    @emilyplanes70823 жыл бұрын

    Please keep posting. Also I have a recommendation. Please add python in your title and thumbnail. You will surely reach more people. Ex. Jump game leetcode python solution #55 - Greedy approach explained Thank you for making these videos.💯❤️

  • @elements8630
    @elements86302 жыл бұрын

    nice video! loved the explanation

  • @city8390
    @city83903 жыл бұрын

    Thanks for your explanation!!

  • @harshavardhini2082
    @harshavardhini2082Ай бұрын

    bro is a genius for coming up with that greedy solution THANK YOUU FOR THIS VIDEO!

  • @lohe2499
    @lohe24992 жыл бұрын

    thank you for clearly explanation this question!!!

  • @AsliArtistVlogs
    @AsliArtistVlogs2 жыл бұрын

    Amazing explanation!

  • @kuzelnet
    @kuzelnet2 жыл бұрын

    Really good video, I was stuck trying to do it in a DP way, but this is really clean! Also, the last line can be simplified to "return goal == 0" as this returns a boolean.

  • @bryanleow5024

    @bryanleow5024

    Жыл бұрын

    this is the dp solution in O(n^2), but only passes 77/170 on LC due to TLE. I think they really want you to use the greedy approach class Solution(object): def canJump(self, nums): dp = [0 for _ in range(len(nums))] # goal can be reached from itself dp[-1] = 1 for i in range(len(nums)-2, -1, -1): for j in range(1, nums[i]+1): # as long as one of your descendants (who u can reach from your current spot) can reach the goal, you can too if (dp[i+j] == 1): dp[i] = 1 # for better efficiency break; return dp[0]

  • @Kaviarasu_NS
    @Kaviarasu_NS5 ай бұрын

    I have started to binge watch Neet ode recently, 2024 is going to be awesome ❤

  • @Raj10185
    @Raj10185 Жыл бұрын

    Fantastic approach love you needcode

  • @Gameplay-ps9ub
    @Gameplay-ps9ub Жыл бұрын

    One small nitpick in the implementation would be, that you could just return goal==0 (which evaluates to boolean value). Some could say using the ternary / if...else is more readable, but it's a matter of opinion I guess. Nevertheless, great video as always. I truly appreciate the way you explain algo, it's very clear imo. :)

  • @marya6244
    @marya62442 жыл бұрын

    Absolutely amazing!!!!

  • @SantoshKumar2
    @SantoshKumar22 ай бұрын

    God bless you! 😀 This helped a lot.

  • @poojasubbu
    @poojasubbu Жыл бұрын

    Amazing! Thank you so much!

  • @adilsaju
    @adilsajuАй бұрын

    thats a unique greedy soln.! awesome!

  • @anjanobalesh8046
    @anjanobalesh80462 жыл бұрын

    The if condition can be modified to If nums [i] >= goal - i i.e if the value at the current index is greater than or equal to the difference between the goal and the index For better understanding 😄 thank you

  • @mattgolden100

    @mattgolden100

    Жыл бұрын

    This really helped me conceptualize. Thank you!

  • @amitdwivedi9951

    @amitdwivedi9951

    Жыл бұрын

    plz exlain hy nums [i] >= goal - i ?

  • @anjanobalesh8046

    @anjanobalesh8046

    Жыл бұрын

    @@amitdwivedi9951 i didn't get your question ??

  • @gunadeepakpallikonda

    @gunadeepakpallikonda

    Жыл бұрын

    Mann!!! That's really helpful 👏🏻 🙂

  • @danmartin1621
    @danmartin1621 Жыл бұрын

    Starting at the end, we always have to traverse the entire length of the array. Starting at the beginning is more performant as we can return true as soon as the current value is > last index - n.

  • @fortuner1122
    @fortuner112211 ай бұрын

    superb video, thanks a ton

  • @Nnemka
    @Nnemka Жыл бұрын

    youre so good! thank you so much

  • @yan-vn5oy
    @yan-vn5oy Жыл бұрын

    brilliant solution!

  • @SM-si2ky
    @SM-si2ky2 жыл бұрын

    I could come up with the DP memorization solution by myself, but got TLE, the greedy solution is optimal but unintuitive.

  • @freindimania11

    @freindimania11

    5 ай бұрын

    Had the same experience, DP with memo got me TLE, however DP with tabulation got through - although still slower than other submissions.

  • @eliabekun
    @eliabekun4 ай бұрын

    Fantastic! I can't believe that is so simple...Amazing!

  • @AmolGautam
    @AmolGautam2 жыл бұрын

    Thank you for the video.

  • @mmeetp
    @mmeetp2 жыл бұрын

    You can skip the last index check by initiating for loop len(nums) -2

  • @vaibhavjaiswal3328
    @vaibhavjaiswal3328 Жыл бұрын

    Best explanation ever!

  • @pratikmhatre4815
    @pratikmhatre4815 Жыл бұрын

    Very good explaination , thanx

  • @anthonyastarita4336
    @anthonyastarita43363 жыл бұрын

    youre amazing keep it up

  • @theantonlulz
    @theantonlulz Жыл бұрын

    Amazing. You made this incredible video explaining the problem and you end it with "return True if goal == 0 else False" instead of just "return goal == 0".

  • @Rajmanov

    @Rajmanov

    5 ай бұрын

    Due to its more comprehensive nature, simplicity doesn't always equate to superiority.

  • @mnchester
    @mnchester2 жыл бұрын

    great video

  • @VikasGupta-ok9lh
    @VikasGupta-ok9lh Жыл бұрын

    You are awesome man

  • @rotemshai3496
    @rotemshai34962 жыл бұрын

    Superb explanation

  • @akhma102
    @akhma102 Жыл бұрын

    Thank you so much!

  • @pekarna
    @pekarna2 жыл бұрын

    Alternative: 1) Start on the right 2) Go to the left, skip all non-zeros 3) On each zero, find any position on the left that can skip it - i.e. larger than the distance from it 4) If there's none, return false 5) If there's any, continue from there, to the point 2) 6) Return true when reaching the left end.

  • @Marcelo-yp9uz

    @Marcelo-yp9uz

    2 жыл бұрын

    I think that's no longer O(n)

  • @ronniey4231
    @ronniey42319 ай бұрын

    Brilliant and elegant!

  • @enesyazici2373
    @enesyazici23733 жыл бұрын

    Great videos, can you also explain how you get the time complexities? I am not how sure how you got the n^n and n^2

  • @robalexnat

    @robalexnat

    2 жыл бұрын

    n^n happens because as for each index number explored, he recursively explores the indices reachable by it, however because he isn't caching it essentially we end up with a lot of repeated work like he mentions. A more visual way of thinking about it: [3,2,1,0,4]. I start with the 3 at index 0, and recursively call (which is the same as a Depth First Search stack implementation) each of the indices reachable (0+1,0+2,0+3) = (1,2,3), I start with index 1. It has a value of 2, same as before, I then have another recursive call to index 2 with value 1, until we reach 0. Now when the stack unfolds, BECAUSE we did not cache, we still end up calling all the branches as before. Meaning when we roll back to index 1 (the one that had value 2), we only explored the first of (1+1,1+2) aka index 2, but we didn't make a call yet for index 3. With Caching this is reduced significantly, and essentially becomes a n + (n-1) + (n-2) +...+1 complexity problem which, when we drop lower terms, we get as O(n^2). Hope this is more clear.

  • @Ren-Ren-Ren-Ren
    @Ren-Ren-Ren-Ren2 жыл бұрын

    I love you Mr. NeetCode.

  • @manoelquirino5081
    @manoelquirino50813 жыл бұрын

    I had a different solution that worked as well. We only can't jump to the end if the array have an element 0 in it and this element isn't the last one. So I loop through every element and check if it was 0, if so, I loop backwards to check if there is an element that can jump over the zero, if so, I'll continue the loop, if not, I'll break and return false

  • @RS-vu4nn

    @RS-vu4nn

    2 жыл бұрын

    He is doing exactly the same thing but more efficiently

  • @aniruddhashahapurkar9244
    @aniruddhashahapurkar92449 ай бұрын

    Thank you!!

  • @yankindeez
    @yankindeez Жыл бұрын

    Thanks!

  • @python_code_nemesis
    @python_code_nemesis8 ай бұрын

    Nice one🎉

  • @annajoen6923
    @annajoen69232 жыл бұрын

    You are the best!

  • @satyadharkumarchintagunta3793
    @satyadharkumarchintagunta3793 Жыл бұрын

    You made problem soo easy Sir!!!

  • @MiravMehtaishere
    @MiravMehtaishere3 жыл бұрын

    Holy Moly , what a piece of cake.

  • @adyamansingh2983
    @adyamansingh298311 ай бұрын

    amazing content

  • @ghzich017
    @ghzich0172 жыл бұрын

    What software do you used to draw? on this scene 9:23

  • @kjplayz3064
    @kjplayz3064Ай бұрын

    hi if it you could only jump a fixed length (e.g. if the number was 2, and you could only jump 2 spaces, not 1), could you still start from the end? Or must yoi use DP?

  • @tejaspoojary5397
    @tejaspoojary53972 жыл бұрын

    Great explanation!! but wont the for loop start from len(nums)-2 ???

  • @cdrrjt5005
    @cdrrjt50052 жыл бұрын

    Just fabulous 🤩🤩

  • @tanaykamath1415
    @tanaykamath14152 жыл бұрын

    loved the soln.!!

  • @siddhantwaghanna4795
    @siddhantwaghanna4795 Жыл бұрын

    For those who have the question of why is always first element chosen as the next goal in greedy approach: There are mainly two types of questions you might be facing: 1. Why always chose the first element as next goal post 2. What if I don't get a further route afterwards by choosing the first element. What if I would have chosen other element that time and I would have gotten answer ( In this case you must have thought what if I couldn't have reached 1 in any way, I would have missed the potential answer of keeping 3 as the next goal.....) Answer: According to the solution, we chose 1 as our goalpost. In the back of our mind we know it can also be reached by 3. you think that I might get stuck on further exploring the path with 1. ** Take a closer look, my friend, if you can reach 4 from 3, you will also definitely reach 1 from 3 ( because 4 is farther away from 1). So while choosing the first element you have the surety that if there is any other potential answer beyond that index, that index could also be reached with that potential answer(in this case 1 could also be reached by 3 as 4 was reachable by 3). And thus you know that you will get the answer by choosing the first element. ** Hope this clears your doubt....

  • @case6339

    @case6339

    Жыл бұрын

    Finally someone answered! Appreciated.

  • @jackie0315
    @jackie0315 Жыл бұрын

    for the greedy explanation, what gives us the right to shift the goal post to the FIRST item that can reach it? There can be multiple items that can reach the goal post that are more "left"? ie in [1,3,2,1,5] we shift the goal from 5 to 1 immediately upon encountering 1, instead of looking further to the left such as 2....why is this greediness guaranteed to produce the correct result?

  • @dinkleberg794
    @dinkleberg7943 жыл бұрын

    Hey Neetcode, do you have any videos on your routine when your were leetgrinding? Like how many questions per day u were doing and how long it took u to complete the 75 questions list?

  • @kneeyaa

    @kneeyaa

    2 жыл бұрын

    I targeted 5 q each day to achieve all in 15 days

  • @haroldobasi2545

    @haroldobasi2545

    2 жыл бұрын

    @@kneeyaa that’s crazy actually

  • @user-ly2mm3kr3n
    @user-ly2mm3kr3nАй бұрын

    Very good video

  • @Rob-in7vp
    @Rob-in7vp Жыл бұрын

    this is so good

  • @jesusromero9167
    @jesusromero9167 Жыл бұрын

    Amazing!

  • @The.Traveling.Nerddd
    @The.Traveling.Nerddd2 жыл бұрын

    you are a boss !!

  • @suryakiran2970
    @suryakiran2970 Жыл бұрын

    super Explanation

  • @ainazikmomunalieva3732
    @ainazikmomunalieva37322 жыл бұрын

    You'r genius!

  • @mohammedfahadnyc1385
    @mohammedfahadnyc13852 жыл бұрын

    Greedy part was fire 🔥

  • @kenhaley4
    @kenhaley4 Жыл бұрын

    Optimization: Notice that you can always jump forward unless you run into a zero. Therefore, just check each zero (if any), and make sure there's a number to the left that's big enough to jump over it. It's a little more code, but much faster, especially if the array is huge but only has a few zeros. Here's my solution: start = 0 while True: try: pos = nums.index(0, start, len(nums) - 1) except ValueError: # no more zeros found break for ptr in range(pos - 1, -1, -1): if nums[ptr] > pos - ptr: break else: return False start = pos + 1 return True

  • @vaibhavbangia7100
    @vaibhavbangia71002 жыл бұрын

    Thanks

  • @shihabrashid5837
    @shihabrashid5837 Жыл бұрын

    O(n^2) DP solution gives TLE, thats pretty sad considering the greedy approach may not be apparent to many in an actual interview.

Келесі