Jump game | Leetcode #55 | Valley peak approach

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ย. 2024
  • This video explains a very important programming interview problem which is to find if it is possible to jump indices of an array and reach the last index of array or not. We have some given constraints which are, we can't jump from 0 valued index and that we always start at index 0. I have shown the solution using backtracking intuition and a very beautiful observation using valley peak approach. I have taken easily comprehend-able examples and finally explained the code for the same. As usual, CODE LINK is given in the description below. If you find any difficulty or have any query then do COMMENT below. This problem is from Day 25 of the leetcode 30 days April coding challenge. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    CODE LINK: gist.github.co...

ความคิดเห็น • 306

  • @techdose4u
    @techdose4u  11 หลายเดือนก่อน

    🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037
    🔴 𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐚𝐥𝐥 𝐨𝐮𝐫 𝐂𝐨𝐮𝐫𝐬𝐞𝐬: techdose.co.in/

  • @preetibatra208
    @preetibatra208 4 ปีที่แล้ว +251

    To the world you may be just youtuber , but for us(learning geeks ";) you are the hero!! great work...

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +17

      Thanks for your appreciation 😅

    • @rajatmishra6628
      @rajatmishra6628 4 ปีที่แล้ว +1

      🙌🏻🙌🏻

    • @MadForCs16
      @MadForCs16 2 ปีที่แล้ว +1

      I thought you would say *To the world you may be just youtuber , but for us(learning geeks ";) you are our world!* xDDD

  • @varunsadhu9060
    @varunsadhu9060 4 ปีที่แล้ว +12

    Dude, I made the mistake to look for a solution for this from other videos. But i must say i could only understand it by your explanation. Keep up the good work buddy.

  • @algorithmimplementer415
    @algorithmimplementer415 4 ปีที่แล้ว +3

    I could not appreciate you more for your initiative of making video each day. BIG THANKS.

  • @saket9363
    @saket9363 4 ปีที่แล้ว +4

    Thank you so much for all your videos! I am learning a lot!
    Just one quick suggestion!
    Can you please show the actual Leetcode question at the start of the video? It would be really helpful! Thanks once again!

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +3

      Actually the questions are easy to understand with lower constraints. So it would be very easy to understand. I read the questions first if it has more constraints.It saves TIME in video length 😅

  • @syamprasadupputalla8233
    @syamprasadupputalla8233 ปีที่แล้ว +3

    Bro, trust me this is some serious content. So far, I have been solving problems with a different mindset and you blew me up. I have seen a lot of videos on Leetcode solutions, but never seen something like this, and explanation is very clear. Thank you so much brother :)

  • @f3-faithfitnessfinance
    @f3-faithfitnessfinance 4 ปีที่แล้ว +5

    Few more subscribers away from 10k!
    Congratulations in advance!🤘🙌
    This is just the beginning 💪😎

  • @AdarshSingh-vt4kw
    @AdarshSingh-vt4kw 2 ปีที่แล้ว +4

    It would be much better if you also tell about the minimum number of steps required to move to at the end of the array

  • @soumitadalal6130
    @soumitadalal6130 4 ปีที่แล้ว +4

    The concepts you teach are so useful that I am bound to adore your vedios.. Keep teaching us like this.. And thanks a lot man..

  • @ersinerdem7285
    @ersinerdem7285 3 ปีที่แล้ว

    wow, just wow! only thing I could add would be, exiting early after the reachable calculation by checking if you can reach the end.

  • @Code_Solver
    @Code_Solver 2 ปีที่แล้ว +5

    What an Explanation. Give a big round of Applause to this sir...

  • @rishabhpathak9916
    @rishabhpathak9916 4 ปีที่แล้ว +11

    What an amazing teacher you are! Thanks a ton!!

  • @shashankmehul02
    @shashankmehul02 4 ปีที่แล้ว +5

    You are great man. Such simplification really helps a lot sometimes.

  • @PersistentProgrammer
    @PersistentProgrammer 4 ปีที่แล้ว +4

    Great explanation. I solved the same question today. I have linked your video in my description.

  • @shridharsarraf2188
    @shridharsarraf2188 ปีที่แล้ว

    Surya pratap sir, jab bhi leetcode se frustrate ho jata hun aapke videos dekhta hun. Aap itna calmly koi bhi problem batate ho aur that also calms me down. Lekin kya karun sir dp hai bhi aisa topic ki itni practice ke baad bhi basic medium questions bhi rula dete hain

  • @ManjilAshaman
    @ManjilAshaman 2 หลายเดือนก่อน

    this solution is just a master peace and you the master 🙌

  • @unlockingitpotential575
    @unlockingitpotential575 2 ปีที่แล้ว

    Your explanation understood KG level student ,that's a great work

  • @abhijeetbhowmik2264
    @abhijeetbhowmik2264 4 ปีที่แล้ว +1

    How do we derive such methods on the spot. I tried this with recursion. Got TLE. Then tried maintaining a list which tells me what index I have already reached out to in previous recursion step to skip it. Then also TLE. And then here it is. Done in O(n). How to achieve this sort of critical thinking. You are awesome.

  • @rhinoara7119
    @rhinoara7119 2 ปีที่แล้ว +2

    Another good approach with O(1) complexity will be start searching for 0 from ending of the array, when a zero is found check next number is greater than 1,if not check if its next greater than 2. Return false if this condition fails until index 0

  • @Jyotigupta-vs4mz
    @Jyotigupta-vs4mz หลายเดือนก่อน

    Hello Tech dose sir,Small corrrection in For Loop i should be i

  • @RaviYadav-xg2hy
    @RaviYadav-xg2hy 4 ปีที่แล้ว +3

    we can also start from ending index of the array and keep updating the required jumps(starting from 1), and finally check for the first element if it has the required number of jumps !!

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Yes correct. Both ways are same only.

  • @MarcoBiedermann
    @MarcoBiedermann 4 ปีที่แล้ว +3

    You could also loop backwards in order to figure out if the last position is reachable
    ```ts
    function canJump(nums: number[]): boolean {
    const { length } = nums;
    let lastPos = length - 1;
    for (let i = length; i >= 0; i -= 1) {
    if (i + nums[i] >= lastPos) {
    lastPos = i;
    }
    }
    return lastPos === 0;
    }
    ```

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yes.....anyone is correct.

    • @haydnmurray6998
      @haydnmurray6998 4 ปีที่แล้ว +1

      I see this solution but worst case for both solutions is O(n) so would we count these as the same?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yes true.

    • @amanbansal97
      @amanbansal97 4 ปีที่แล้ว

      It will take less time to complete because there is no comparison of max

  • @charanpasupula3763
    @charanpasupula3763 3 ปีที่แล้ว

    After for loop I think we still need to have another if condition to check if reachable >= array.length - 1
    otherwise this code is failing for the case of [1,0,1], do correct me if m wrong
    edit: I was wrong in writing this condition in for loop i < array.length - 1 , it should be i < array.length

  • @manjunathp9466
    @manjunathp9466 4 ปีที่แล้ว +2

    Python solution
    class Solution:
    def canJump(self, nums: List[int]) -> bool:
    if 0 not in nums:
    return True
    reachable=0
    for i in range(len(nums)):
    if reachable

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Thanks for sharing 👍 Please keep sharing Codes in Python.

    • @ladydimitrescu1155
      @ladydimitrescu1155 2 ปีที่แล้ว

      Can you explain the purpose of if 0 not in nums:
      return True?

  • @vibhasyadav6491
    @vibhasyadav6491 2 ปีที่แล้ว

    @TECH DOSE please make a video on minimum jumps to reach the end.

  • @codingwithneeraj1305
    @codingwithneeraj1305 2 ปีที่แล้ว

    exceptional approach have you thought of this approach on your own??

  • @gurpreetchawla2066
    @gurpreetchawla2066 3 ปีที่แล้ว +13

    Whenever i am not getting clear Explaination of a problem in other channel , i jump to this channel and you solves it in such a way that anyone can understand comfortably .
    You got the way of "How to Teach so that anyone can understands".

  • @rajendertanwar1881
    @rajendertanwar1881 3 หลายเดือนก่อน

    This explanation truly deserves ❤ 👍

    • @techdose4u
      @techdose4u  3 หลายเดือนก่อน

      Thanks :)

  • @paragroy5359
    @paragroy5359 3 ปีที่แล้ว +2

    Nice explanation sir...thanks for the video

  • @mystryb345
    @mystryb345 4 ปีที่แล้ว +1

    Bro your every video has very good explanation.It is easy to understand

  • @6priyanshu
    @6priyanshu 2 ปีที่แล้ว

    very well explained...please keep uploading more such video's.....such explanation makes coding easier and interesting..thankyouuuuuuuuu

  • @sagarksahoo4667
    @sagarksahoo4667 3 ปีที่แล้ว +2

    explanation was very crystal clear, good work

  • @nikhilkumar-ot9rn
    @nikhilkumar-ot9rn 4 ปีที่แล้ว +2

    awesome explanation!!!! can u start a platform where u take questions from us and solve it ...please..

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      It's already ON. I am putting questions which are already requested.

    • @nikhilkumar-ot9rn
      @nikhilkumar-ot9rn 4 ปีที่แล้ว

      @@techdose4u ok then, could you please solve Educational Codeforces Round 53 (Rated for Div. 2) problem c (vasya and robots) also could you please solve longest palindromic substring using binary search .

  • @AKASHSINGH-td7wx
    @AKASHSINGH-td7wx 3 ปีที่แล้ว

    can you please give me the code of that backtracking approach u mention at the beginning...?

  • @venkateshthirunagiri85
    @venkateshthirunagiri85 4 ปีที่แล้ว +2

    Make a video on your TH-cam kit like video editors, softwares, Mike u use to make TH-cam videos .......... 🎥🎥

  • @sajithtm4864
    @sajithtm4864 ปีที่แล้ว

    Great explanation. Glad to see you posting more videos on DS.

  • @JgNt3981
    @JgNt3981 ปีที่แล้ว

    How do you get the logic for the problems sir, that too simple one. 😱🙏 please share if you could.

  • @vivek.tiwary
    @vivek.tiwary 2 ปีที่แล้ว

    How many problems you solved to get the depth of this kind of problem? No one explained like you did. Thank you

  • @dhanashreegodase4445
    @dhanashreegodase4445 3 ปีที่แล้ว +1

    I have no words to say ..lovely excellent video

  • @venkateshthirunagiri85
    @venkateshthirunagiri85 4 ปีที่แล้ว +3

    We or you are going to Hit 10k subscribers by tomorrow 👏👏👏👏

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +4

      We offcourse. Tommorow I will come LIVE to thank our community for continued support :)

    • @venkateshthirunagiri85
      @venkateshthirunagiri85 4 ปีที่แล้ว +2

      @@techdose4u you deserved it 😎

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Thanks :)

  • @fullysimplified7139
    @fullysimplified7139 3 ปีที่แล้ว

    I can never thanks you enough as thanks is such a small word
    I wish you all the best in your future journey

  • @ekengineer9868
    @ekengineer9868 2 ปีที่แล้ว +1

    So cool explaination!
    Impressed 🙌

  • @avinashpatil9662
    @avinashpatil9662 3 ปีที่แล้ว +1

    good explaination..totally understood the problem and its solution.

  • @KaranDoshicool
    @KaranDoshicool 4 ปีที่แล้ว +2

    How to calculate number of steps?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Store it when you find a path. Keep updating on each call.

  • @swatibabbar9953
    @swatibabbar9953 3 ปีที่แล้ว +1

    How to find minimum jumps to reach end of array with this code?

    • @rakshith3547
      @rakshith3547 3 ปีที่แล้ว

      Even i had the same question

  • @hinata4661
    @hinata4661 4 ปีที่แล้ว

    please upload the min jumps problem using dynamic programming. If already uploaded please share the link

  • @navinshyamkuwar9401
    @navinshyamkuwar9401 5 หลายเดือนก่อน

    Very Good Explanation.

  • @deepaksingh3533
    @deepaksingh3533 24 วันที่ผ่านมา

    i can go for solution only when i get what exactly a problem is, after watching this i still not able to understand what exactly this problem is asking.

  • @tareqdevdiary
    @tareqdevdiary 8 หลายเดือนก่อน

    No explanation can be better than this.

    • @shubhamkumar-hx1fb
      @shubhamkumar-hx1fb 8 หลายเดือนก่อน

      if you think so then you are leaving in some virtual world

    • @tareqdevdiary
      @tareqdevdiary 8 หลายเดือนก่อน

      For me it's the best explanation. I've understood the solution after watching this, before that I watched multiple videos from other youtubers but have found this one the best.@@shubhamkumar-hx1fb

  • @suryakiran2970
    @suryakiran2970 ปีที่แล้ว +1

    last approach super

  • @toekneema
    @toekneema 3 ปีที่แล้ว +1

    Best solution ever

  • @kollivenkatamadhukar5059
    @kollivenkatamadhukar5059 4 ปีที่แล้ว +1

    Can you please guide me on how to approach a problem, I always try to solve a problem but will fail in between and after seeing your explanation after enough trails I feel like this can be done but why I didn't.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      This requires practice. Don't give up. I was in the same position as you. I struggled and invested a lot of time before I reach to my current position. Even now I face the same problem solving the harder problems but I take the same approach for solving.

  • @AruneshSrivastava
    @AruneshSrivastava 4 ปีที่แล้ว +2

    We dont have to loop through the entire array in some cases . like [1,3,2,0,2] ... in this we can reach end at 2nd index itself , we can have a check like -- if (reachabe == n ) retrun true . save some time maybe

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      If you increase checks then actual time will increase instead of decreasing.

  • @SonuKumar-kj6uz
    @SonuKumar-kj6uz 3 ปีที่แล้ว

    Please upload minimum number of jumps to reach end....🙏🙏

  • @NipunAggarwal-h9l
    @NipunAggarwal-h9l 5 หลายเดือนก่อน

    its very easy and cool explanantion

  • @praphulyadav4471
    @praphulyadav4471 3 ปีที่แล้ว +1

    sir your explanation is awesome it becomes so easy to understand❤️👍

  • @scarywitcher
    @scarywitcher ปีที่แล้ว +1

    finally understood brother. thanks to you

  • @akshatchouhan5199
    @akshatchouhan5199 3 ปีที่แล้ว

    Buddy can you explain this question using DP

  • @alltechsimplified2134
    @alltechsimplified2134 2 ปีที่แล้ว

    how you come up with these approaches

  • @monishnjs
    @monishnjs 3 ปีที่แล้ว +1

    Wow, what a nice solution...thanks

  • @indrajith2002
    @indrajith2002 11 หลายเดือนก่อน

    bro you really explain with clarity just love it .Keep going

  • @Safar_Ayushi_Ka
    @Safar_Ayushi_Ka 4 ปีที่แล้ว +1

    Sir please make video on min jumps using DP

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yea sure. I told you in this video that I will include it in future. It is jump game 2. I will do it don't worry.

  • @bestdeal3385
    @bestdeal3385 2 ปีที่แล้ว +1

    thankuu sir you made this very easy 😃😃

  • @willturner3440
    @willturner3440 3 ปีที่แล้ว

    You again proof you are great, love u bro

  • @creatorstudio5945
    @creatorstudio5945 ปีที่แล้ว

    Amazing explanation sir :))

  • @mmsamar1007
    @mmsamar1007 3 ปีที่แล้ว +2

    can you write the brute force code

  • @harshpatel1385
    @harshpatel1385 4 ปีที่แล้ว +1

    After this series plz make video on how to crack codevita exam

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Achha okay. I need to see it once how is the pattern.

  • @PrashantSingh-pg9vq
    @PrashantSingh-pg9vq 3 ปีที่แล้ว +1

    should we bow? yes, he is a king !! ...

  • @575saisri4
    @575saisri4 4 ปีที่แล้ว +2

    sir, u r really the best.. thank u so muchhhh..

  • @Utkarshgg
    @Utkarshgg ปีที่แล้ว

    reachable

  • @ayushdhiman8128
    @ayushdhiman8128 2 ปีที่แล้ว

    leetcode have one test case i.e. [2,5,0,0] and by my own approach I stuck at this. isn't this test case wrong?

  • @vaibhavgaur8200
    @vaibhavgaur8200 3 ปีที่แล้ว +1

    JAVA Solution:
    class Solution {
    public boolean canJump(int[] nums) {
    int n = nums.length;
    int reachable = 0;
    for(int i=0; i

  • @swami1016
    @swami1016 3 ปีที่แล้ว +1

    well explaind

  • @saunaknandi1814
    @saunaknandi1814 2 ปีที่แล้ว

    Is valley peak a greedy approach?

  • @simmi641
    @simmi641 7 หลายเดือนก่อน

    To the point explanation thank you ❤

  • @alex-gz7ud
    @alex-gz7ud 3 ปีที่แล้ว +1

    Awesome explanation!!!! You saved my day!!!

  • @emmatime2016
    @emmatime2016 3 ปีที่แล้ว +2

    Great explanation! Thank you!

  • @rbk.technology4747
    @rbk.technology4747 ปีที่แล้ว

    Really amazing

  • @mausami16mau
    @mausami16mau 4 ปีที่แล้ว +2

    well explained. thanks :)

  • @AdityaRaj-ey6tq
    @AdityaRaj-ey6tq ปีที่แล้ว

    how is [2,0,0] coming true

  • @sandeepnallala48
    @sandeepnallala48 3 ปีที่แล้ว

    Sir for this q dp is not necessary right?
    I did the q of min jumps to reach end of array , and tried this in similar wau, but getting tle in leetcode, maybe some code mistake.. this simple method is good sir. Sir have you started dp sir?

  • @CHIRANJIBNANDY1
    @CHIRANJIBNANDY1 4 ปีที่แล้ว +1

    Dude... just an awesome explanation.

  • @dimpleshah3037
    @dimpleshah3037 4 ปีที่แล้ว +1

    Superb explaination....

  • @monkeytrollhunter
    @monkeytrollhunter 4 ปีที่แล้ว +12

    I am here after my recursive solution timed out

  • @jaydeepmahajan6598
    @jaydeepmahajan6598 4 ปีที่แล้ว +1

    This is eventually dp because we are first checking for smaller inputs 😅, am i right ?

  • @mystryb345
    @mystryb345 4 ปีที่แล้ว +1

    Bro plz cover what min jump array also

  • @thehorizonreport
    @thehorizonreport 3 ปีที่แล้ว +1

    Literally amazing hero

  • @rajbhushan3541
    @rajbhushan3541 11 หลายเดือนก่อน

    good explanantion

  • @avishekarora
    @avishekarora 2 ปีที่แล้ว

    That's a really god solution

  • @hoyinli7462
    @hoyinli7462 3 ปีที่แล้ว +1

    excellent explanation

  • @reshwanthroyal2601
    @reshwanthroyal2601 ปีที่แล้ว

    class Solution {
    public boolean canJump(int[] nums) {
    int reach = 0;
    for(int i =0;i=nums.length-1)
    return true;
    }
    // if we cant reach
    return false;
    }
    }

  • @liftwithamey
    @liftwithamey 4 ปีที่แล้ว +1

    could you please provide recursive/backtracking code?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I will make separate recursion video when I start backtracking.

    • @liftwithamey
      @liftwithamey 4 ปีที่แล้ว +1

      I will be waiting!!!

  • @pritishpattnaik4674
    @pritishpattnaik4674 2 ปีที่แล้ว

    Very much well explained

  • @yaswanthp2294
    @yaswanthp2294 2 ปีที่แล้ว

    Again excellent explanation

  • @letsdoeverythinginoneweek9398
    @letsdoeverythinginoneweek9398 3 ปีที่แล้ว

    how can we find max jumps to reach at end of array

  • @rohitkanojiya5880
    @rohitkanojiya5880 3 ปีที่แล้ว +1

    Sir, Great Explaination Thanks

  • @bhopalsingh4102
    @bhopalsingh4102 4 ปีที่แล้ว +1

    I tried using memoization... i got TLE for a testcase having values as [24500,24499,24998,24997,24996,....].

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I din't try memoization but simple recursion. I passed 70/75 tc. So I applied the efficient O(N) approach 😅 DP will also work.

    • @bhopalsingh4102
      @bhopalsingh4102 4 ปีที่แล้ว

      @@techdose4u same here..
      BTW.. excellent explanation👍👍

  • @mayanksikarwal8431
    @mayanksikarwal8431 4 ปีที่แล้ว +1

    Wonderful

  • @retro-re-play
    @retro-re-play 2 ปีที่แล้ว

    This problem is much simpler if you start checking from behind:
    Use formula: if nums[i] >= len(nums) - 1 - i
    To see if you can reach end of nums from an index i.
    Start iterating from the end-1 and check if you can reach the end from i index, if yes then mark the i as the end (slice the nums = nums[:i+1]).
    Now in furthur iterations you need to check if you can reach the new end, if yes again update the end.
    If any point in loop if you can reach end from i, and i == 0 then return true

  • @funtalk4083
    @funtalk4083 4 ปีที่แล้ว +1

    Thanks sir. Great explanation.