Greedy Algorithm - Jump Game - Leetcode 55

Поделиться
HTML-код
  • Опубликовано: 29 май 2024
  • FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

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

  • @GregHogg
    @GregHogg  2 месяца назад +7

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @yashwanthbm7543
    @yashwanthbm7543 2 месяца назад +37

    Thank you RUclips for recommending these types of videos

  • @Stepbrohelp
    @Stepbrohelp 2 месяца назад +32

    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 2 месяца назад

      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 2 месяца назад

      ​@@pickaboo1697ayoo i was trying exactly same thing.... Tf

    • @GregHogg
      @GregHogg  2 месяца назад +1

      You've got this :)

    • @IRGhost0
      @IRGhost0 Месяц назад

      Ask step bro.

  • @pewpew069
    @pewpew069 2 месяца назад +44

    Ohh yes, the classic if ....: return True else: return False

  • @allan.suzuki
    @allan.suzuki 2 месяца назад +1

    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  2 месяца назад

      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 2 месяца назад +1

    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 2 месяца назад +5

    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  2 месяца назад +1

      This sounds like a cool solution

    • @mazthespaz1
      @mazthespaz1 Месяц назад +1

      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

  • @nagisa5848
    @nagisa5848 2 месяца назад +1

    Looks DP enough 😅, o(n) is also workable

  • @devpratap7322
    @devpratap7322 Месяц назад +1

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

  • @RmAndrei93
    @RmAndrei93 Месяц назад

    Why not res := len(arr) % arr[0] ; if res != 0 { return false } else { return true } ?

  • @Sa3vis
    @Sa3vis 2 месяца назад +9

    How's this faster/easier than going forward?

    • @yassinesafraoui
      @yassinesafraoui 2 месяца назад +2

      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 2 месяца назад +2

      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 Месяц назад +1

      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.

  • @sgetti4dinner
    @sgetti4dinner 2 месяца назад

    Would a greedy algorithm still work if you had to return the shortest path?

    • @1llum1nate
      @1llum1nate 2 месяца назад

      Sort of. You could do a linear BFS-ish solution by noticing that the shortest distance and monotonic and increasing by 1 at max

  • @mprone
    @mprone Месяц назад +4

    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

  • @NMjaga
    @NMjaga Месяц назад

    Sir,what if the array have [2,2,2,4]?

  • @sasham4073
    @sasham4073 2 месяца назад +3

    this guy reminds me of my ex boss.. he used to do insanely complex things where solution was super simple

    • @schlutzzz
      @schlutzzz 2 месяца назад

      What the fuck is he doing bragging about a 56th percentile solution

    • @GregHogg
      @GregHogg  Месяц назад +1

      It would be much faster given larger values of N

  • @vatsalsaxena4002
    @vatsalsaxena4002 2 месяца назад

    Should you not start the iteration from n-2?

    • @GregHogg
      @GregHogg  2 месяца назад

      I suspect you could also do that yes

  • @hlubradio2318
    @hlubradio2318 Месяц назад

    Dint have time

  • @pavanprasad3061
    @pavanprasad3061 Месяц назад

    I can't understand this someone help please😭

  • @Chromenx
    @Chromenx 2 месяца назад +1

    what even?

  • @cnote4554
    @cnote4554 2 месяца назад +3

    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 2 месяца назад +1

      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

  • @harimbolaramilison6845
    @harimbolaramilison6845 2 месяца назад

    where can I find these exercises?

  • @DevAdityaGupta
    @DevAdityaGupta Месяц назад

    Dance at the end

    • @GregHogg
      @GregHogg  Месяц назад +1

      I love dancing

  • @asagiai4965
    @asagiai4965 2 месяца назад +1

    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  2 месяца назад +1

      It won't loop endlessly, and it turns out you really only need to consider the max jump

    • @asagiai4965
      @asagiai4965 2 месяца назад

      I see.my bad. I thought somehow, the goal or max_jump will affect i

    • @vamsipreetham6224
      @vamsipreetham6224 2 месяца назад

      For the second test cases while the same code return true but its actually is false

  • @tushar404
    @tushar404 2 месяца назад

    Not as easy as it looks
    🙄

  • @user-ji6ip7ou8d
    @user-ji6ip7ou8d 2 месяца назад +1

    #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 2 месяца назад

      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 2 месяца назад

      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 2 месяца назад

      @@dontlike1000 Are you sure, because I think both versions will return false?

    • @dontlike1000
      @dontlike1000 2 месяца назад

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