Тёмный

Greedy Algorithm - Jump Game - Leetcode 55 

Greg Hogg
Подписаться 168 тыс.
Просмотров 37 тыс.
50% 1

FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

Опубликовано:

 

29 май 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 64   
@GregHogg
@GregHogg Месяц назад
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@yashwanthbm7543
@yashwanthbm7543 Месяц назад
Thank you RU-vid for recommending these types of videos
@Stepbrohelp
@Stepbrohelp Месяц назад
Just spent an hour yesterday trying to solve this exact problem and couldn’t get it. Hopefully one day I’ll be experienced enough to be able to make stuff like this look as easy as you do
@pickaboo1697
@pickaboo1697 Месяц назад
Lmao same, I was just looking for zeros and see if any element to it's left has more value than it's position w.r.t zero
@AdityaBahuguna-yn7me
@AdityaBahuguna-yn7me Месяц назад
​@@pickaboo1697ayoo i was trying exactly same thing.... Tf
@GregHogg
@GregHogg Месяц назад
You've got this :)
@IRGhost0
@IRGhost0 29 дней назад
Ask step bro.
@pewpew069
@pewpew069 Месяц назад
Ohh yes, the classic if ....: return True else: return False
@darrenxavierjohan861
@darrenxavierjohan861 Месяц назад
return goal == 0
@allan.suzuki
@allan.suzuki Месяц назад
I honestly had the intuition on what you did. But I couldn't write the logic so fluently. Thanks for the video and the inspiration ❤️
@GregHogg
@GregHogg Месяц назад
That's amazing :)
@alxjones
@alxjones Месяц назад
This works because we are given a _max_ jump length, not a fixed one. So anytime we can get to a goal from another space, any solution that jumps over that space could choose not to and just land there. So, once we've determined that we have a path to the goal from some spot, whether or not there is a path to that goal is _equivalent_ to whether or not there is a path to the final goal. But if we can _only_ jump the number at that point (or there is some other restriction to how we can jump), this may not be true, as it may be that all possible paths to the end goal skip over some node from which you could reach the goal.
@Pevi70
@Pevi70 Месяц назад
Thanks for the video! I think you could do it starting at the beginning and also consider values of 0 in the array which could cause an infinite loop def canJump: i = 0 while(i = len(nums) or nums[i] == 0: return False return False
@GregHogg
@GregHogg Месяц назад
Did you test this?
@emilfrei6303
@emilfrei6303 Месяц назад
Why don’t you look for the 0 in your array and count the distance to this index and check wether your key at that index is larger than the distance to the 0?
@GregHogg
@GregHogg Месяц назад
This sounds like a cool solution
@mazthespaz1
@mazthespaz1 Месяц назад
and what if there are multiple zeros?
@emilfrei6303
@emilfrei6303 Месяц назад
@@mazthespaz1 you do that for every zero
@mazthespaz1
@mazthespaz1 Месяц назад
@@emilfrei6303 code it. let's see it. seems like you can make it work, but will not be as efficient. prove me wrong
@sgetti4dinner
@sgetti4dinner Месяц назад
Would a greedy algorithm still work if you had to return the shortest path?
@1llum1nate
@1llum1nate Месяц назад
Sort of. You could do a linear BFS-ish solution by noticing that the shortest distance and monotonic and increasing by 1 at max
@nagisa5848
@nagisa5848 Месяц назад
Looks DP enough 😅, o(n) is also workable
@GregHogg
@GregHogg Месяц назад
Yep!
@Sa3vis
@Sa3vis Месяц назад
How's this faster/easier than going forward?
@yassinesafraoui
@yassinesafraoui Месяц назад
I also thought to start from the beginning and move forward, and thought I'd do it differently than he did, I think it's the same, computationally it should give same results and performance, if you don't screw up the algorithm of course
@user-lp7gr3oc9c
@user-lp7gr3oc9c Месяц назад
Yea I got the same question. Since he can start from 0. In the for loop he can read the element value to fast forward the i value. And if it reaches the last element of the array just return true
@norabseven
@norabseven Месяц назад
You can't really go forward because it's "MaxJumps" so you can jump fewer and thus would have several available jump positions per index that you need to follow. Backwards makes sure that you can reach the end via any route.
@realquaker3488
@realquaker3488 Месяц назад
Not only can you go forward, but it'll be even faster than "backwards" algorithm. Basically you calculate maximal reacheble index from each position while keeping global max reach. Something like that: i = 0 max_i = arr[i] while ++i = len - 1 return true return false
@norabseven
@norabseven Месяц назад
Consider an array that is [5,100,1,1,1,1,1,1,0,0,0,0,0,0,0,1] You can't just blindly add on the found element at the index as you'd likely get caught in a trap. In the array you would add on the 5 and skip over the 100 trapping you behind the zeros as there is only single jumps remaining.
@RmAndrei93
@RmAndrei93 Месяц назад
Why not res := len(arr) % arr[0] ; if res != 0 { return false } else { return true } ?
@devpratap7322
@devpratap7322 Месяц назад
This is what I did, starting from the 0'th index check if you have still moves left , if yes then move forward and update the moves: bool canJump(vector& nums) { int n=nums.size(); int i=0; int posi=1; while(posi!=0) { if(i==n-1)return true; posi--; posi=max(posi,nums[i]); i++; } return false; }
@vatsalsaxena4002
@vatsalsaxena4002 Месяц назад
Should you not start the iteration from n-2?
@GregHogg
@GregHogg Месяц назад
I suspect you could also do that yes
@cnote4554
@cnote4554 Месяц назад
I feel like this a bad solution because while it works, it doesn't follow the instructions of the question which is to start at the array's first index. You could solve this as well by initializing a variable at 0 that is the current furthest index you can reach in the array. Then using a for loop (i) with range 0 to n-1 and incrementing by 1, you can set the furthest as the max() of the current furthest index or i + nums[i]. At that point while still in the loop you can check to see if the furthest index is equal to the current index i and return False if they are equal. If you get through the loop you can return True.
@polycrystallinecandy
@polycrystallinecandy Месяц назад
The "start at index 0" refers to where the jumping sequence starts, not where your program must start. The program is not the jumping sequence, the program is a feasibility check
@NMjaga
@NMjaga Месяц назад
Sir,what if the array have [2,2,2,4]?
@GregHogg
@GregHogg Месяц назад
It returns true
@sasham4073
@sasham4073 Месяц назад
this guy reminds me of my ex boss.. he used to do insanely complex things where solution was super simple
@schlutzzz
@schlutzzz Месяц назад
What the fuck is he doing bragging about a 56th percentile solution
@GregHogg
@GregHogg Месяц назад
It would be much faster given larger values of N
@harimbolaramilison6845
@harimbolaramilison6845 Месяц назад
where can I find these exercises?
@blackmuskveetandoor2487
@blackmuskveetandoor2487 Месяц назад
Leetcode...its a website
@blackmuskveetandoor2487
@blackmuskveetandoor2487 Месяц назад
Or codeforces
@mprone
@mprone Месяц назад
Solution's fine, but if I were the interviewer I'd have complained about that final if-else block, which is a "clean code red flag". How to properly do it: `return goal == 0;`
@GregHogg
@GregHogg Месяц назад
Yeah I wrote this on purpose to try and make it clearer for some folks but I think it just backfired lol
@asagiai4965
@asagiai4965 Месяц назад
Idk but this code looks weird. And it also possible that it loops endlessly. I'm not exactly sure though. Also max jump, doesn't it mean, you can also jump lower than the max jump?
@GregHogg
@GregHogg Месяц назад
It won't loop endlessly, and it turns out you really only need to consider the max jump
@asagiai4965
@asagiai4965 Месяц назад
I see.my bad. I thought somehow, the goal or max_jump will affect i
@vamsipreetham6224
@vamsipreetham6224 Месяц назад
For the second test cases while the same code return true but its actually is false
@DevAdityaGupta
@DevAdityaGupta Месяц назад
Dance at the end
@GregHogg
@GregHogg Месяц назад
I love dancing
@pavanprasad3061
@pavanprasad3061 Месяц назад
I can't understand this someone help please😭
@Chromenx
@Chromenx Месяц назад
what even?
@hlubradio2318
@hlubradio2318 Месяц назад
Dint have time
@tushar404
@tushar404 Месяц назад
Not as easy as it looks 🙄
@user-ji6ip7ou8d
@user-ji6ip7ou8d Месяц назад
#include #include bool can_jump(int *arr, int len) { if (!arr || len = len) return false; } return false; } void print_bool(bool v) { if (v) { printf("True"); return; } printf("False"); } int main() { int arr[] = {2, 3, 1, 1, 4}; print_bool(can_jump(arr, 5)); int arr2[] = {3, 2, 1, 0, 4}; print_bool(can_jump(arr2, 5)); return 0; }
@Pevi70
@Pevi70 Месяц назад
I think I have something similar to yours, but then the python version. It also includes the scenario of example 2 where you end up on the value 0
@dontlike1000
@dontlike1000 Месяц назад
Seems like your algorithm is not correct. Try array [2,3,0,4]. His algorithm will return true, but, as far as I can see, yours will return false at second step inside while.
@Pevi70
@Pevi70 Месяц назад
@@dontlike1000 Are you sure, because I think both versions will return false?
@dontlike1000
@dontlike1000 Месяц назад
@@Pevi70 Yes, I've checked author's python code with [2,3,0,4]. It returns true (and it is correct).
@Pevi70
@Pevi70 Месяц назад
@@dontlike1000 Are you sure that is correct, because it will never reach the last index?
Далее