#Leetcode

Sdílet
Vložit
  • čas přidán 20. 08. 2024
  • Given an array of non-negative integers nums, you are initially positioned at the first index of the array.
    Each element in the array represents your maximum jump length at that position.
    Your goal is to reach the last index in the minimum number of jumps.
    You can assume that you can always reach the last index.
    Example 1:
    Input: nums = [2,3,1,1,4]
    Output: 2
    Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
    Example 2:
    Input: nums = [2,3,0,1,4]
    Output: 2

Komentáře • 111

  • @arpitgaurav4222
    @arpitgaurav4222 Před 11 měsíci +12

    Got deviated with the hillarious sneaky movement behind 🤣🤣🤣🤣🤣. Great Explanation

  • @sanjayjoshi8807
    @sanjayjoshi8807 Před 3 lety +16

    Life would have been easy if greedy had easy proofs: Nice explanation though !!

  • @vyankateshkulkarni4374
    @vyankateshkulkarni4374 Před 2 lety +13

    I was struggling to understand this question from last 2 days. i've watched ur video 3-4 times and now its clear to me. thanks for such a good explanation

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

    1:21 someone sneaked into your room :P
    Concept is Crystal clear, thanks

  • @gitanjalikumari9262
    @gitanjalikumari9262 Před měsícem +2

    Thanks for easy and nice explanation 😊

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

    Honestly the way u explained i subscried your channel😁 took more than 5 video to understand this logic Thanks

  • @mansisethi8127
    @mansisethi8127 Před 2 měsíci

    Thank you Alisha !! This problem had become a nightmare for me. I couldn't find it's right solution. Finally your solution helped. This is such a simple solution

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

    Her video should be on the top of the search results. Really great explanation.

  • @Movie_clips43456
    @Movie_clips43456 Před 4 měsíci +1

    Honestly mam appka samghane ka tarika ekdam nique hai thanku mam

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

    let me add a trick i.e, add the arrowed line on the given place. this will prevent unnecessary iteration once the current and further has reached the last array element. and the previous jump is considered the optimal jump state.
    for(int i = 0; i < n-1; i++){
    --> if(curr==far && far>=nums.length-1) break;
    far = Math.max(far, (i + nums[i]));

    • @niravpanchal11
      @niravpanchal11 Před 6 měsíci +1

      This condition is only going to be true when loop counter (i) reach the end of an array. Hence, I don't think to add this condition for reducing the iteration or further calculations in loop.

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

    This is such an underrated solution to this problem throughout the youtube 🙏🙏

    • @adityatiwari8930
      @adityatiwari8930 Před rokem

      better u ignore. at least she is trying to help and Alisha plz ignore such bull shits....

    • @vishalplayzz2580
      @vishalplayzz2580 Před rokem +1

      @@adityatiwari8930 kya hua bhai underrated matlab kuch glt nai hai

    • @gaurimandot5788
      @gaurimandot5788 Před rokem

      @@adityatiwari8930 Tumne misunderstand kar diya comment ko. @lakshyasharma1940 ka matlab hai ki itne ache solution ka rating kaafi kam hai youtube par.

  • @paneercheeseparatha
    @paneercheeseparatha Před rokem +2

    Salute to your dedication🙏🙏

  • @ridhamkaria8771
    @ridhamkaria8771 Před rokem +1

    I had solved this question using graph.
    void BFS(vector adj[],vector &visited,vector &dist,int start){
    queue q;
    q.push(start);
    dist[start]=0;
    while(!q.empty()){
    int node=q.front();
    q.pop();
    visited[node]=1;
    for(auto i:adj[node]){
    if(!visited[i]){
    visited[i]=1;
    q.push(i);
    dist[i]=dist[node]+1;
    }
    }
    }
    }
    int jump(vector& nums) {
    int n=nums.size();
    if(n

  • @sumitghewade2002
    @sumitghewade2002 Před 10 měsíci

    Aunty was so cute, tried her best not be in the frame. Thanks for awsome explanation.

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd Před 2 lety +2

    Thanks a lot for such a great explanation!! Keep posting more video on Leetcode daily challenge.

  • @ksansudeen
    @ksansudeen Před 2 lety

    The intuition of problem at 6.20 .. great!!!!

  • @AkashSharmaSDE
    @AkashSharmaSDE Před rokem

    Easiest and best solution among all

  • @marq_8976
    @marq_8976 Před 5 měsíci

    This is is the best solution for me.

  • @Manishgupta200
    @Manishgupta200 Před 6 měsíci +1

    Good explaination

  • @RaselAhmed-ix5ee
    @RaselAhmed-ix5ee Před 2 lety +2

    we have to break the loop if current reaches the last index , else one extra jump will be added!

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

    Thanks mam, It took me 1 day to solve

  • @AlphaWomen-d2h
    @AlphaWomen-d2h Před 17 dny

    The one condition is not their that is if(i >= furthest) {return -1 } bz we cannot move forward.

  • @RomanReigns-tg5qm
    @RomanReigns-tg5qm Před 3 lety

    finally a video with good explanation..

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

    I understand the solutin after 3 to 4 dry run.

  • @VenkatGopalakrishnan
    @VenkatGopalakrishnan Před 5 měsíci

    if the jump is not possible then we can have a check . see below.
    class Solution:
    def minJumps(self, array, n):
    if n == 0 or array[0] == 0:
    return -1
    jumps= current = farthest = 0
    for i in range(n-1):
    farthest = max(farthest, array[i] + i)
    if i >= farthest:
    return -1
    if i == current:
    current = farthest
    jumps += 1

    return jumps

  • @balajijr07
    @balajijr07 Před 10 měsíci +1

    Am i the only guy for whom the code is not working?

  • @anikaithkapoor4223
    @anikaithkapoor4223 Před rokem

    very good solution. good explanation

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

    crystal clear concept thank u :)

  • @721lakhvirsingh2
    @721lakhvirsingh2 Před rokem

    thanks mam,you cleared my doubts ,i was struggling with this question

  • @kunalkheeva
    @kunalkheeva Před rokem +3

    why did you run the loop until arr.size()-1 instead of arr.size()

    • @PIYUSH-lz1zq
      @PIYUSH-lz1zq Před rokem

      have got this ??

    • @SaadKhan-sg4wh
      @SaadKhan-sg4wh Před 8 měsíci +1

      @@PIYUSH-lz1zq bcuz if u go through the last element, the condition if(i==curr) will satisfy and jump++ will execute so you get an extra jump than required. Do a dry run to understand better.

  • @prabhakarmishra1966
    @prabhakarmishra1966 Před rokem

    literally good explanation

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

    Nice Explanation

  • @shrustis5240
    @shrustis5240 Před rokem +1

    When you are not able to reach the last index, return -1, slight changes in code as shown below
    int farthest = 0, jumps = 0, curr = 0;
    //edge case
    if(arr[0] == 0)
    return -1;
    for(int i=0; i

    • @shivarajpatil8699
      @shivarajpatil8699 Před rokem

      In the question it's written that we will reach the last index anyhow. so this piece of code is not needed.

  • @CodeBeans.i0s
    @CodeBeans.i0s Před rokem

    Thanks - for easy explanation...

  • @nikhilsatyam4815
    @nikhilsatyam4815 Před rokem

    thanks di your explanation was superb

  • @pranavpranjal2717
    @pranavpranjal2717 Před 3 měsíci

    why do we iterate only until the second last index ?can you explain this?

  • @anshulrawat3458
    @anshulrawat3458 Před 7 měsíci

    just a correction u have not added condition if i > current: return -1 , which is one of the edge case..

  • @ramyasruti9874
    @ramyasruti9874 Před 2 lety

    Thanks for the explanation. But this is failing on 44th test case in Leetcode: [1,2,3].

  • @vibhavsharma2724
    @vibhavsharma2724 Před rokem

    Nice explanation . Thanks👍

  • @02deepak
    @02deepak Před rokem

    IIB respect 🙇‍♀

  • @surajrawat6730
    @surajrawat6730 Před rokem

    easiest approach 💙💙

  • @ShivamYadav-in2jg
    @ShivamYadav-in2jg Před rokem

    Thanks nicely explained

  • @lakshsachdeva3907
    @lakshsachdeva3907 Před 8 měsíci

    beautiful explanation

  • @asmitshukla4649
    @asmitshukla4649 Před 10 měsíci

    i think the intuition u gave that "current" will point to the last made optimal jump is not correct as because
    for test case [4,1,1,3,1,1,1] the current will be pointing to indexes 0,4,6 which are not the correct positions to take a jump on...although the answer of jumps will be still 2 luckily and hence it passed that test case, so could u please explain the above test case?????????/

  • @sunilsinghrathore7825

    pretty good explanation

  • @kshitijsingh9311
    @kshitijsingh9311 Před 2 lety

    Very nice explanation mam. Thank you

  • @talhachinchali7188
    @talhachinchali7188 Před 10 měsíci

    U got my subscribe❤

  • @rakshith3547
    @rakshith3547 Před 3 lety

    Nice explanation, do upload more such videos..

  • @mayankashtekar6074
    @mayankashtekar6074 Před rokem

    thanks a lot for such a great explanation

  • @preetyrebari7691
    @preetyrebari7691 Před 2 lety

    such a great explanation, thanksss!

  • @rishirachamalla5885
    @rishirachamalla5885 Před 3 lety

    Really really good ...please increase example numbers ...like if any edge cases are there and some tips

  • @RiteshSingh-yp1sm
    @RiteshSingh-yp1sm Před 7 měsíci

    Nice One

  • @vishalsiddha6637
    @vishalsiddha6637 Před 2 lety

    mashallaha mashallah solution Thanks!

  • @josevieraflores8312
    @josevieraflores8312 Před rokem

    really good explained

  • @priyankanarekar7983
    @priyankanarekar7983 Před rokem

    really helpful😊

  • @saurabhkunwar3692
    @saurabhkunwar3692 Před 11 měsíci

    why dp is not working here

  • @aniketjaiswal3876
    @aniketjaiswal3876 Před 3 lety

    Well explained!!, :)

  • @bhooviarora519
    @bhooviarora519 Před 4 měsíci

    I==current means????

  • @PIYUSH-lz1zq
    @PIYUSH-lz1zq Před 2 lety +1

    Can you please explain what does i==current means ??? When I and curr is becoming same ??

    • @vishalgupta957
      @vishalgupta957 Před 2 lety

      after going through all the possible cases if we have reached the last case, then we need to update maxReach and jump as well.

  • @suyashverma2526
    @suyashverma2526 Před rokem

    mummy rocks!!!!!!

  • @em_ashutosh
    @em_ashutosh Před 7 měsíci +1

    why size-1?

    • @em_ashutosh
      @em_ashutosh Před 7 měsíci

      lol got it we dont want to go anywhere from last index

  • @namratadhobale7051
    @namratadhobale7051 Před 2 lety

    just amazing

  • @a4abhishekkmr
    @a4abhishekkmr Před rokem

    Thanks

  • @TejasSharma-gu8br
    @TejasSharma-gu8br Před 11 měsíci

    nice

  • @reshmaparveen6679
    @reshmaparveen6679 Před 3 lety

    Eagaely I am waiting for you ❤️❤️

  • @ishaankulkarni49
    @ishaankulkarni49 Před rokem

    why do u go till

  • @jimsitu5511
    @jimsitu5511 Před 2 lety

    Why i < nums.size() - 1 but not < nums.size(), why it stops two indexes before?

    • @probabilitycodingisfunis1
      @probabilitycodingisfunis1  Před 2 lety +10

      Thank you Jim, for asking this
      In this question, it's given you can always reach the last index
      Consider the example [2,1,1,1]
      The number of jumps you need to take is only 2 here
      First jump at 0
      Second jump at 2
      So two times you go in the case (i==current) & increment jumps
      If you take i< nums.size()
      You will consider the jumps starting from the last index also
      (when i==3) you will enter in the case (i==current)and again increase jumps and make it to 3 which is wrong

  • @gypsygirlkigypsylife
    @gypsygirlkigypsylife Před rokem

    why num.sum()-1,and when we have to return true or false then only it is num.size()?????

    • @SaadKhan-sg4wh
      @SaadKhan-sg4wh Před 8 měsíci

      bcuz if u go through the last element, the condition if(i==curr) will satisfy and jump++ will execute so you get an extra jump than required. Do a dry run to understand better.

  • @india_vswar4384
    @india_vswar4384 Před rokem

    [0 , 1, 1, 1, 1] output of this is 1. But and is -1.can u help me to solve this issue.

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

      if u get 0 jumps at initial then there is no chance to jump to next step so u can write this edge case if(nums[0]==0)return 0

  • @devsagar5812
    @devsagar5812 Před rokem

    can anyone explain me that why we're traversing given array till the n-2 idx?

    • @anishaa3298
      @anishaa3298 Před 11 měsíci

      i checked with chatgpt, it said:
      I changed the loop condition from for (int i = 0; i < nums.length; i++) to for (int i = 0; i < n - 1; i++) to iterate until the second-to-last element, as you only need to consider reaching the last element as a destination.
      in other words, our aim is to reach the last element, therefore we only need to iterate till the second last element.

    • @SaadKhan-sg4wh
      @SaadKhan-sg4wh Před 8 měsíci

      bcuz if u go through the last element, the condition if(i==curr) will satisfy and jump++ will execute so you get an extra jump than required. Do a dry run to understand better.

  • @rounakdutta6164
    @rounakdutta6164 Před 2 lety

    if the curr becomes >= lastIndex, we can break from the for loop, then there will be fewer number of iterations. a little more optimized.

  • @aryankumar87771
    @aryankumar87771 Před rokem +1

    1:22 peeche dekho peeche 😂

    • @PIYUSH-lz1zq
      @PIYUSH-lz1zq Před rokem

      why did you run the loop until arr.size()-1 instead of arr.size()

  • @harshitsharma5647
    @harshitsharma5647 Před rokem

    Explaination is superb but if the array elements is like
    arr = [2 1 0 3]
    then this code is not work.

    • @PIYUSH-lz1zq
      @PIYUSH-lz1zq Před rokem +1

      why did you run the loop until arr.size()-1 instead of arr.size()

    • @shivarajpatil8699
      @shivarajpatil8699 Před rokem +1

      The question say the test cases will be such that we will reach the end.

  • @namratadhobale7051
    @namratadhobale7051 Před 2 lety

    please take more example

  • @rabindradocument8934
    @rabindradocument8934 Před 2 lety

    Steps vs Jumps

  • @abhinavTyagi5665
    @abhinavTyagi5665 Před 11 měsíci

    Aunty at 1:21 😂

  • @arshdeep011
    @arshdeep011 Před rokem

    Thanks di

  • @sagrikasoni7685
    @sagrikasoni7685 Před 2 lety

    Thanku ❤️❤️

  • @namratapatil8692
    @namratapatil8692 Před 2 lety

    Thanks for the Explanation .
    4
    2 1 0 3
    for this i/p , output is -1 but code is giving 2
    i think code will give wrong o/p if for any (a[i]+i) is giving largest index but the value of that index is 0 so our output will be -1
    how can we tackle this??

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

      You can assume that you can always reach the last index. ->REad ques carefuly

    • @H_2So_4
      @H_2So_4 Před 2 lety

      int farthest=0;
      int current=0;
      int jumps=0;
      for(int i=0;i=n-1) break;
      if(current==i){
      current=farthest;
      jumps++;
      }
      }
      if (n==1) return 0;
      //cout

    • @googlyboy018
      @googlyboy018 Před rokem +1

      In that case in loop give another if statement that if at any point your farthest is less than i then return -1 .. i.e
      If(farthest

  • @Vishaljoshi-uo6yc
    @Vishaljoshi-uo6yc Před rokem

    we have to run our loop for (

  • @010amityadav7
    @010amityadav7 Před rokem

    if possible please teach in Hindi

  • @sauravchandra10
    @sauravchandra10 Před rokem

    Kuch samaj nahi aaya