DP 3. Frog Jump | Dynamic Programming | Learn to write 1D DP

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ย. 2024

ความคิดเห็น • 2.1K

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

    I need your support, and you can do that by giving me a like, and commenting "understood" if I was able to explain you.

  • @KaushalDhruw
    @KaushalDhruw 5 หลายเดือนก่อน +80

    2 years later. Still the ultimate and unbeatable learning resource in the market.

    • @salmanpandey8150
      @salmanpandey8150 4 หลายเดือนก่อน +2

      But i thought adita verma was best for dp

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

      ​@@salmanpandey8150yrr usne to mujko kisi pe bhi recursion karna bata diya bina soche uska base case nikap deta hun

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

    The dp series is like web series. Every day a new episod is coming with lots of new things and leaving a suspence for the next one.. thank you striver for this wonderful effort..and "UNDERSTOOD"..

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

      only if we could get a trailer for the next day's video😂

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

    This series is so damn good that before watching this video, i thought of giving "min cost climbing stairs" question on leetcode a try because i felt i could do that. That is similar to this question only. And boom, i solved that question in all possible ways...recursion, memoization, tabulation and space optimised method. Thank you striver. You have kinda opened our brain and made our thinking broader. Thank you so much for the inspiration you are. Love you !!!

  • @jayantmishra6897
    @jayantmishra6897 ปีที่แล้ว +30

    this is the only playlist on youtube where you will find only positive comments because this man really put his effort to make this playlist valuable.
    thank you so much striver.

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

      i can see 2, striver and love babbar

    • @jeet-smokey
      @jeet-smokey 3 หลายเดือนก่อน

      @@manikantadevadiga7146 Love Babbar copies from striver. I have seen many videos taking the same sample code and examples from striver.

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

      @@manikantadevadiga7146 Aditya Verma ka bhi dekh lo.

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

    After searching all over the world to grasp these DSA concepts, it wasn't until I watched this video that everything finally clicked. The way you explained it was beyond exceptional-thank you for creating such a clear and impactful resource!

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

    I recently started this dp series and i want to complete this series in 20 days this is my day 1
    Thank you for this awesome series

  • @026harshagarwal9
    @026harshagarwal9 2 ปีที่แล้ว +23

    We will take a for loop running from j=1 to k and instead of writing (ind-1) and (ind-2) we will going to simply write (ind-j) that will give me the solution.
    Time Complexity:-O(N*K)
    Space complexity:-O(N).
    Yes sir, it cannot be space optimised because we have to use K+1 variables . Nice video and yes you are absolutely right, I have learnt space optimisation in dp from you
    THANKS MAN 👍👍

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

      baniya buddhi mat chalao jyada striver bhai ko appreciate kaaro khud toh kuch kar nahi raahe

    • @Anonymous-lw4nq
      @Anonymous-lw4nq ปีที่แล้ว

      @@cipherCrafters14 XD

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

    Amazing video! Do watch till the end because in the space optimization part, the actual value of watching the past two videos comes.

  • @sumitsanu9524
    @sumitsanu9524 ปีที่แล้ว +9

    You will get hired even if you can explain an interviewer till memoization thing but it will blow the mind of interviewer if you go till space optimisation to constant.... awesome video and striver is just god.

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

    Learnt the concept, went straight to the editor, coded it in JAVA and submitted it...All test cases submitted 🔥👍
    Striver's way of explaining recursion and DP is unbeatable and uncomparable

  • @PujaKumari-zl7rw
    @PujaKumari-zl7rw 2 ปีที่แล้ว +12

    Understood, Honestly this is the very first time I am trying to learn any dp problem. And I am able to understand everything... Very clear explanation. Loved it.. Thanks a lot for all the efforts :)

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

    Hey Striver!
    I tried solving this question myself and I was able to solve it, I must say this series is amazing. You started building my concepts in first 2 lectures itself. Gonna see your lecture now and look at your approach. Would surely complete whole dp series.
    Thank you for this series ❤

  • @adebisisheriff159
    @adebisisheriff159 8 หลายเดือนก่อน +7

    Thanks so much Striver. We really appreciate your effort. You are a rare gem.

  • @alisharath161
    @alisharath161 10 หลายเดือนก่อน +4

    Best video I have ever seen on DP. Thank you Striver for helping out fellow developers.

  • @ryanmathew6397
    @ryanmathew6397 10 หลายเดือนก่อน +2

    your explanation is so good that even before you write the pseudo code, i was able to write the recursion by starting from 0 to n-1 rather than the reverse approach that you have taken in the video. Great Work you are doing. Helps to improve the way a person can solve a problem.

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

    Recursion code at 24:33
    Memoization code at 25:25
    Tabulation code at 30:54
    Space optimization at 36:22

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

      Thanks dude

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

      Would recommend to watch completely without skipping though
      The insights given by him are great

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

      @@yournemesis8232 Yes, you are right. This is just for jumping to the desired explanation without wasting time - when you are revising

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

    follow ups solution:
    #include
    int frogJump(int n, vector &heights, int k){
    vector dp(n, INT_MAX);
    dp[0] = 0;
    for(int i=1; i

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

      this is what i thought in the very first second.

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

      @@letslearn1055 me too

    • @user-id7pz9bs2f
      @user-id7pz9bs2f 11 หลายเดือนก่อน +1

      @@letslearn1055 same here when he said k jumps then i thought of a nested for loop till k

    • @uditgarg6508
      @uditgarg6508 6 หลายเดือนก่อน

      mee too

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

    Code Of Frog Jump
    #include
    using namespace std;
    //Top-Down Approach
    int dp1(int ind,vector&heights,vector&dp)
    {
    if(ind == 0) return 0;
    if(dp[ind] != -1) return dp[ind];
    int left = dp1(ind-1,heights,dp) + abs(heights[ind]-heights[ind-1]);
    int right = INT_MAX;
    if(ind > 1) right = dp1(ind-2,heights,dp) + abs(heights[ind]-heights[ind-2]);
    return dp[ind] = min(left,right);
    }
    //Tabulation Method - Bottom Up Approach
    /*
    vectordp(n,0);
    dp[0]=0;
    for(int i=1;i 1) secondstep = dp[i-2] + abs(heights[i] - heights[i-2]);
    dp[i] = min(firststep,secondstep);
    }
    return dp[n-1];
    */
    //Space Optimization
    /*
    int prev=0;
    int prev2=0;
    for(int i=1;i1) secondstep = prev2+abs(heights[i]-heights[i-2]);
    int curi = min(firststep,secondstep);
    prev2 = prev;
    prev = curi;
    }
    return prev;
    */
    int frogJump(int n, vector &heights)
    {
    vectordp(n+1,-1);
    return dp1(n-1,heights,dp);
    }

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

    Hey, striver
    i just want to say that concept in your videos are such a crystal clear and easy to understand that even i am studying for the first time i do not feel any difficulty to understand the solutions.
    so, thank you so much for providing such a video lecture free on youtube

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

    UNDERSTOOD , Now I am loving dp its become easy to think from recursion to tabulation step by step, thank you so much for the hardwork!

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

    Amazing video! Everything is clearly understandable. from day 1 of dp series I have watching your videos and Day by Day become big fan of your teaching way. God bless you sir. Came to know from your insta story that u have fever 🤒 Get well soon sir.

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

      Yes his teaching is absolutely marvelous and I become big fan of him he keep rocking now I am happy to be cse student 😇😇

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

    Understood!! After seeing the recursion, I was able to convert it on my own into a dp. Thank you STRIVER. ❤

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

      You need to learn Recursion bro

  • @studynewthings1727
    @studynewthings1727 11 หลายเดือนก่อน +5

    The code for k jumps:
    #Memoization
    def k_jumps(n,dp,arr,k,result):
    if n == 0:
    return 0
    if dp[n] != -1:
    return dp[n]
    for i in range(1,k+1):
    if (n > (i - 1)):
    step = k_jumps(n-i,dp,arr,k,result) + abs(arr[n] - arr[n-i])
    else:
    step = 9999999
    result = min(result,step)
    dp[n] = result
    return dp[n]
    arr = [30,10,60,10,60,50]
    n = len(arr)
    k = 3
    result = 999999
    dp = [-1 for i in range(n)]
    print(k_jumps(n-1,dp,arr,k,result))
    #Tabulation
    def k_jumps(n,arr,k):
    dp = [-1 for i in range(n)]
    if n == 0:
    return 0
    dp[0] = 0
    for index in range(1,n):
    result = 999999
    for i in range(1,k+1):
    if (index > (i - 1)):
    step = dp[index-i] + abs(arr[index] - arr[index-i])
    else:
    step = 9999999
    result = min(result,step)
    dp[index] = result
    print(dp)
    return dp[n-1]
    arr = [60,30,10,60,10,60,50]
    n = len(arr)
    k = 3
    result = 999999
    print(k_jumps(n,arr,k))

  • @kanhaiyaverma7391
    @kanhaiyaverma7391 9 วันที่ผ่านมา

    understood and I was always afraid of dp and now it looks easy.... Thank you!

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

    Understood! Your series is making me love DP. Thanks a ton! Excited for the next one.

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

    At 25:23, can't you initialize the dp array for only n elements instead of n+1? Like you did for tabulation?

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

    "UNDERSTOOD".....
    Bhaiya pls upload practice problem link other than coding ninja link. Leetcode interface is best for easy understanding.
    I hope you understand , Thanks

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

      Its not there on leetcode(Same problem)

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

      Sadly leetcode is missing these kind of problems. No platform can match leetcode interface

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

      A similar problem on Leetcode is Min cost Climbing stairs 746

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

      @@zaidshaikh2536 bro i didnt ask for a similar problem on leetcode, I asked Striver bro to give the leetcode link and not the coding ninja link.

  • @Harmless_creature
    @Harmless_creature 18 วันที่ผ่านมา +1

    00:03 Find the minimum total energy used by the frog to reach the first stair
    02:13 The minimum energy required to jump from one point to another is 20.
    06:11 Recursion is a way to find the path with minimum energy.
    08:30 The costing for this guy will be zero no matter what you do.
    12:51 Applying memoization to optimize recursion
    14:47 Return the minimum of the left and right subpoints
    18:51 Recursion helps solve multiple sub problems by reusing previously solved solutions.
    20:50 Convert a recursion solution to dynamic programming
    24:48 Memoization and tabulation are two approaches to solve problems recursively and iteratively respectively.
    26:43 The first step in tabulation is to initialize the dp array.
    30:55 The space complexity can be optimized by using the index minus 1 and index minus 2 technique
    32:49 Use variables instead of an array to store previous values.
    36:49 The problem requires k jumps as a follow-up to i plus 1 and i plus 2.
    38:38 The ultimate resource for placements

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

    this is the only playlist on youtube where you will find only positive comments because this man really put his effort to make this playlist valuable.
    thank you so much striver.
    ❤❤From Gujarat !! Jordar (Gujarati)!

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

    Can we write base case for n=1, then can write Math.abs(heights[1]-heights[0])?

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

    If possible pls provide leetcode problem link also of the particular problem since most of us are following leetcode

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

      Similar problem on Leetcode is 746

  • @Vishal-joshi1998
    @Vishal-joshi1998 2 ปีที่แล้ว +40

    Count all possible palindrome in string with rearranging characters - Infosys 🙄

    • @SumitChauhan-vv5ix
      @SumitChauhan-vv5ix 6 หลายเดือนก่อน +2

      This can be solve using combinatrix also , right??😅

  • @dEviL_bIsWaJiT
    @dEviL_bIsWaJiT 2 หลายเดือนก่อน +1

    Understood, Thanks a lot bhaiya ❤

  • @manideep7148
    @manideep7148 15 วันที่ผ่านมา

    I did not see this series feeling that DP is soo tough but after listening to your explanation, I've changed my mind. All credits to you Striver !

  • @parvathivanapilli8572
    @parvathivanapilli8572 6 หลายเดือนก่อน +1

    Understood, Now I'm loving Dp Thank you Striver ❤

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

    Approach:
    Since the frog can make k jumps, we can use k recursive calls and find the minimum of them. used a variable ans to kept it as positive infinity to update the answer . base case is same as the frog jumps qn. but since frog can make k jumps, used a for loop to find the
    the answer for every jump (1st , 2nd ,.... till kth). also a check of i-w>=0 is made so that there is no infinite recursion.
    def frogJump(n,heights,k) -> int:
    i=n-1
    ans=float('inf')
    def helper(i,ans):
    if i==0:
    return 0
    for w in range(1,k+1):
    if i-w>=0:
    find=helper(i-w,ans)+abs(heights[i-w]-heights[i])
    ans=min(find,ans)
    return ans
    return helper(i,ans)
    def frogjump_tabulation(n,heights,k):
    dp=[0]*(n+1)
    dp[0]=0
    for i in range(1,n):
    ans=float('inf')
    for j in range(1,k+1):
    if i-j>=0:
    find=dp[i-j]+abs(heights[i-j]-heights[i])
    ans=min(ans,find)
    dp[i]=ans
    return dp[n-1]

  • @ArjunSehgal-gv1cc
    @ArjunSehgal-gv1cc 6 หลายเดือนก่อน +1

    semester 1 is about to start,and i am on this lecture,super confident>3000

  • @vibhavsharma2724
    @vibhavsharma2724 4 หลายเดือนก่อน +1

    understood this very well. Thanks to striver

  • @AjaySingh-ty2wm
    @AjaySingh-ty2wm 8 หลายเดือนก่อน +1

    JavaScript/ typescript all 4 ways
    // simple recursion
    function f(ind: number, heights: number[]): number {
    if (ind == heights.length - 1) return 0;
    let left = f(ind - 1, heights) + Math.abs(heights[ind] - heights[ind - 1]);
    let right = Infinity;
    if (ind > 1) right = f(ind - 2, heights) + Math.abs(heights[ind] - heights[ind - 2]);
    return Math.min(left, right);
    }
    // recusion with memoization
    function memoF(ind: number, heights: number[], dp: number[]): number {
    if (ind == heights.length - 1) return 0;
    if (dp[ind] != -1) return dp[ind];
    let left = memoF(ind - 1, heights, dp) + Math.abs(heights[ind] - heights[ind - 1]);
    let right = Infinity;
    if (ind > 1) right = memoF(ind - 2, heights, dp) + Math.abs(heights[ind] - heights[ind - 2]);
    return dp[ind] = Math.min(left, right);
    }
    // tabulation
    function TabulationFrogJump(n: number, heights: number[]): number {
    let dp = new Array(n + 1).fill(0);
    dp[0] = 0;
    dp[1] = Math.abs(heights[1] - heights[0]);
    for (let i = 2; i 1) right = dp[i - 2] + Math.abs(heights[i] - heights[i - 2]);
    dp[i] = Math.min(left, right);
    }
    return dp[n];
    }
    // space optimised tabulation
    function SpaceOptimisedTabulationFrogJump(n: number, heights: number[]): number {
    let prev1 = 0;
    let prev2 = Math.abs(heights[1] - heights[0]);
    for (let i = 2; i

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

    Understood!! I was always scared of Dynamic Programming but you made it so easy 🔥

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

    I managed to do the space optimization on my own! That's just how good your explanation is!

  • @SoyaibZihad10
    @SoyaibZihad10 9 หลายเดือนก่อน +1

    I am nothing but greatful. Wanna finish the whole playlist. Thanks a lot

  • @athangkulkarni8245
    @athangkulkarni8245 9 หลายเดือนก่อน +1

    Understood!
    Also, I could do tabulation and space optimization on my own after watching the explanation! A very big Thank you🥺

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

    your intention is to make education easier...yes!! striver you did it....We learn from you.We will make you proud a Day...thankyou striver for this wonderful series..this is first comment on my youtube..try to ping my comment if u have time ❤

  • @rohan8758
    @rohan8758 5 หลายเดือนก่อน +1

    understood. Thanks for DP web series with addiction

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

    I also started understanding dsa , you are magical man of programming world✌

  • @stith_pragya
    @stith_pragya 8 หลายเดือนก่อน +1

    UNDERSTOOD...........😎😎😎😎😎😎

  • @stith_pragya
    @stith_pragya 8 หลายเดือนก่อน +1

    Thank You Much for this wonderful video.............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

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

    Amazing explanation. Perfect solution to explain in an interview..

  • @vibhorbhatt3076
    @vibhorbhatt3076 22 วันที่ผ่านมา

    This question became a piece of cake for the one's following previous vedios.
    Thankyou Striver Bhaiya.

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

    US striver! TUF always takes us forward...No one can beat striver in explaining the problem

  • @prabhakaran5542
    @prabhakaran5542 7 หลายเดือนก่อน +1

    Understood ❤

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

    K jumps code:
    int memomization(int index,vector &h)
    {
    vector dp(index+1,0);
    int fs=0;int ss=INT_MAX;
    for(int i=1;i=0)
    ss=dp[i-k-1]+abs(h[i-k-1]-h[i]);
    smallest=min(smallest,min(fs,ss));
    dp[i]=smallest;
    }
    }


    }
    return dp[index-1];
    }

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

    Bro the way u explained is just amazing

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

    Thank you vai. Its really awesome. And love from Bangladesh

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

    Very helpful and minute problems were explained nicely. Nothing best than this.

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

    Code for K jumps :
    int f(int idx, int k, vector &heights, vector &dp)
    {
    if (idx == 0)
    {
    return 0;
    }
    if (dp[idx] != -1)
    {
    return dp[idx];
    }
    int minCost = INT_MAX;
    for (int i = 1; i = 0)
    {
    int jumpCost = f(idx - i, k, heights, dp) + abs(heights[idx] - heights[idx - i]);
    minCost = min(minCost, jumpCost);
    }
    }
    return dp[idx] = minCost;
    }
    int frogJump(int n, int k, vector &heights)
    {
    vector dp(n + 1, -1);
    return f(n - 1, k, heights, dp);
    }

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

    Frog jump with k jumps:
    #include
    using namespace std;
    int min_dp(int n, vector &heights, int ind, vector &dp,int k) {
    if (ind == 0)
    return dp[0]=0;
    if (ind == 1)
    return dp[1] = abs(heights[ind] - heights[ind - 1]);
    if (dp[ind] == -1) {
    vector temp(k);
    for(int i=1 ; i> n>>k;
    vector heights(n);
    for (int i = 0; i < n; i++)
    {
    cin >> heights[i];
    }
    int ind = n-1;
    vector dp(n + 1, -1);
    cout> t;
    while (t--)
    {
    solve();
    }
    return 0;
    }

  • @ADITYASINGH-b4u
    @ADITYASINGH-b4u 2 หลายเดือนก่อน

    understood every bit of content ...thank u for this amazing lecture

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

    Regular Recursion using java:
    public static int frogJump(int n, int heights[]) {
    return dpRecursion(n - 1, heights);
    }
    private static int dpRecursion(int n, int[] heights) {
    if (n == 0) return 0;
    int left = dpRecursion(n - 1, heights) + Math.abs(heights[n] - heights[n - 1]);
    int right = n > 1 ? dpRecursion(n - 2, heights) + Math.abs(heights[n] - heights[n - 2]) : Integer.MAX_VALUE;
    return Math.min(left, right);
    }

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

    US😎 Before the lecture, i was having a lot of fear about tabulation. Bt now its totally clear.😇 Thanks Raj Bhaiyya

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

    this space optimization technique is way too classic, thanks bhaiya

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

    #include
    int frogJump(int n, vector &heights)
    {
    if(n == 0) return n;
    int left = frogJump(n-1,heights) + abs(heights[n] - heights[n-1]);
    int right;
    if(n > 1)
    {
    right = frogJump(n-2,heights) + abs(heights[n] - heights[n-2]);
    }
    return min(left,right);
    }
    whats wrong?

  • @BiswajitDas-lk7pp
    @BiswajitDas-lk7pp 7 ชั่วโมงที่ผ่านมา

    UNDERSTOOD 🥰

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

    Take the base case of index == 1 as return abs(heights[1]-heights[0]), makes the code a bit simpler without if else conditions.

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

      index == 0 condition stays as it is so basically two base cases

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

    I think it is the best solution to previous videos

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

    time complexity and space complexity of memoization, 22:20

  • @abytespaceneeded
    @abytespaceneeded 9 หลายเดือนก่อน

    Solution for Frog Jump:
    #include
    #include
    #include
    #include
    using namespace std;
    // recursion
    // Time Complexity: O(2^N)
    // Space Complexity: O(N) - stack space
    int helper(vector& nums, int index) {
    if(index == 0) return 0;
    int step1 = abs(nums[index] - nums[index-1]) + helper(nums, index-1);
    int step2 = INT_MAX;
    if(index - 2 >= 0) {
    step2 = abs(nums[index] - nums[index-2]) + helper(nums, index-2);
    }
    return min(step1, step2);
    }
    int forgJump(vector& nums) {
    int size = nums.size();
    return helper(nums, size-1);
    }
    // memoization
    // Time Complexity: O(N)
    // Space Complexity: O(N) + O(N) - stack space
    int helper(vector& nums, int index, vector& dp) {
    if(index == 0) return 0;
    if(dp[index] != -1) return dp[index];
    int step1 = abs(nums[index] - nums[index-1]) + helper(nums, index-1, dp);
    int step2 = INT_MAX;
    if(index - 2 >= 0) {
    step2 = abs(nums[index] - nums[index-2]) + helper(nums, index-2, dp);
    }
    return dp[index] = min(step1, step2);
    }
    int forgJump(vector& nums) {
    int size = nums.size();
    vector dp(size, -1);
    return helper(nums, size-1, dp);
    }
    // Tabulation
    // Time Complexity: O(N)
    // Space Complexity: O(N)
    int forgJump(vector& nums) {
    int size = nums.size();
    vector dp(size, 0);
    dp[0] = 0;

    for(int i=1;i= 0) {
    step2 = abs(nums[i] - nums[i-2]) + dp[i-2];
    }
    dp[i] = min(step1, step2);
    }
    return dp[size-1];
    }
    // Space Optimized
    // Time Complexity: O(N)
    // Space Complexity: O(1)
    int forgJump(vector& nums) {
    int size = nums.size();
    int prev = 0, secondPrev = 0;
    for(int i=1;i= 0) {
    step2 = abs(nums[i] - nums[i-2]) + secondPrev;
    }
    int curr = min(step1, step2);
    secondPrev = prev;
    prev = curr;
    }
    return prev;
    }

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

    Thanks a lot!!!! I was able to solve the video without seeing the video all because of you!!!!

  • @user-to3jj8lj8h
    @user-to3jj8lj8h 2 หลายเดือนก่อน

    Understood ! much appreciations to your efforts .

  • @sidhantsingh4200
    @sidhantsingh4200 3 วันที่ผ่านมา +1

    understood !

  • @SethuIyer95
    @SethuIyer95 6 หลายเดือนก่อน

    I am actually watching this to give a google interview sometime later but god damn, this is actually fun to learn lol

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

    Understood each step in detail 💯

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

    "UNDERSTOOD" Great series Striver

  • @vikrammehra4354
    @vikrammehra4354 3 หลายเดือนก่อน +1

    thanku striver bhaiya

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

    Understood! Thanks for explaining beautifully!

  • @lifehustlers164
    @lifehustlers164 6 หลายเดือนก่อน +1

    4/57 done!!! understood!

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

    Understood. Amazing explanation. Thanks a lot for making these videos.

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

    Understood! hauuwa bana rakha tha dp ka 1 saal se man mein
    thanks 900

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

    Understood. Great Playlist !!!!

  • @LowkeyCoder
    @LowkeyCoder 9 หลายเดือนก่อน

    "understood"
    i came here with a doubt that i am from java if i will be able to understand or not but what happened i understood as fine as i can and i can write the same in c++ with little bit of help oviously but thanks a lot !

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

    Best explanations indeed, love you man.

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

    big thank you bhai for your hard work and make questions easy for us , again thank you with saying "we can't be lazy by seeing your high level of energy to teach us", "UNDERSTAND" 😍🥰🤩😇😘❤💘

  • @ashj1165
    @ashj1165 19 วันที่ผ่านมา

    understood;
    possibly wont have to watch this topic again :)

  • @Happy-tf7se
    @Happy-tf7se 4 หลายเดือนก่อน

    As I am declaring the dp array globally it is giving incorrect answer. What could be the reason??
    vector dp(100000,INT_MAX);
    int solve(int ind, vector& height){
    if(ind==0) return 0;
    if(dp[ind]!=INT_MAX) return dp[ind];
    int jumpTwo = INT_MAX;
    int jumpOne= solve(ind-1, height)+ abs(height[ind]-height[ind-1]);
    if(ind>1)
    jumpTwo = solve(ind-2, height)+ abs(height[ind]-height[ind-2]);

    return dp[ind]=min(jumpOne, jumpTwo);
    }
    int frogJump(int n, vector &heights)
    {
    // Write your code here.
    return solve(n-1,heights);

    }

  • @20arpitbhatnagarit7
    @20arpitbhatnagarit7 ปีที่แล้ว

    Instead of if clause sir you could have inserted the val of dp[1] and started loop from i = 2

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

    Understood!! Thanks a lot for creating this series.

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

    UNDERSTOOD!!!

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

    Thank you sir
    Understood

  • @G.Lakshmi_Prasanna
    @G.Lakshmi_Prasanna ปีที่แล้ว

    Clearly understood💯. Thank you so much for the series

  • @fanofabdevillersandmathslo5960
    @fanofabdevillersandmathslo5960 หลายเดือนก่อน +1

    Understood

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

    NICE SUPER EXCELLENT MOTIVATED

  • @gauraviitkharagpur6594
    @gauraviitkharagpur6594 2 หลายเดือนก่อน +1

    Understand

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

    Explanation is very high level❤❤❤

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

    Understood Striver Sir !!

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

    very clear explanation

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

    Understood...Completed 3/56

  • @abhinavthodupunuri2464
    @abhinavthodupunuri2464 หลายเดือนก่อน +1

    understood

  • @abhisheknemade2444
    @abhisheknemade2444 9 หลายเดือนก่อน

    Thanks baiya very wonderfull video. I understood the video. Please make more and more videos. God will bless you. HK.