House Robber | Recursion | Memo | Bottom Up | Leetcode 198

Поділитися
Вставка
  • Опубліковано 12 вер 2024
  • This is the 9th Video on our Dynamic Programming (DP) Playlist.
    In this video we will try to solve another very very famous DP Problem "House Robber" also known as "Stickler Thief"
    We will try to understand how we come up with the DP approach. The intuition behind it. And see time and space complexity.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Problem Name : House Robber
    Company Tags : Amazon, OYO Rooms, Paytm, Walmart, Google, Flipkart, LinkedIn, Airbnb
    My solutions on Github : github.com/MAZ...
    Leetcode Link : leetcode.com/p...
    My GitHub Repo for interview preparation : github.com/MAZ...
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
    #interviewpreparation #interview_ds_algo #hinglish

КОМЕНТАРІ • 93

  • @manishv.8167
    @manishv.8167 Рік тому +26

    Really good way of explanation,
    Waiting for the day when you will start DSA lectures on UA-cam that will bring disaster for sure 🤘🤘

  • @chinmaykotkar8456
    @chinmaykotkar8456 Рік тому +12

    time flys away watching this superb explanation, Carry on with this 💪💪

  • @atifakhtar8828
    @atifakhtar8828 2 місяці тому +2

    bhaiya jaisa aapne bottom up samjhaya hai na maza aa gaya ..hazaro videos dekhe lekin kabhi samajh nhi aaya lekinn aapne samjha diya

  • @priyanshupandey3148
    @priyanshupandey3148 Рік тому +6

    Can't wait to see your lectures on DSA.

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Soon
      Thanks a lot for watching Priyanshu ❤️❤️❤️

  • @khalidalam980
    @khalidalam980 8 місяців тому +2

    One suggestion!
    The bottom up approach can be space optimized from SC(n) to SC(1). As we are utilizing only previous two max values, it can only be stored in two integer variables.
    CPP solution:-
    class Solution {
    public:
    int rob(vector& nums) {
    int n = nums.size();
    if(n == 1) return nums[0];

    int prev2 = 0; // house array [prev2, prev1, current_house]
    int prev1 = nums[0];
    int maxsteal = 0;
    for(int i=2;i

    • @codestorywithMIK
      @codestorywithMIK  8 місяців тому

      Definitely. Thanks a lot for sharing ❤️🙏

  • @wearevacationuncoverers
    @wearevacationuncoverers Рік тому +2

    This is by far the best channel I found who goes to all the depth of any problem. Thanks a lot man

  • @flyingcolor5159
    @flyingcolor5159 Рік тому +4

    Bhai ka mast samjate ho app maja aagaya. 💝🚀

  • @NiteshYadavnitw
    @NiteshYadavnitw 22 дні тому +2

    your video is always on priority.

  • @piyushacharya7696
    @piyushacharya7696 Рік тому +2

    Katai lajawab.😎
    the first approach i thought of without properly understanding the qn but seeing the constraints was this which is wrong.
    class Solution {
    public int rob(int[] nums) {
    int maxi = Integer.MIN_VALUE;
    for (int i = 0; i < nums.length; i++) {
    int sum = 0;
    for (int j = i; j < nums.length; j += 2) {
    sum += nums[j];
    }
    maxi = Math.max(maxi, sum);
    }
    return maxi;
    }
    }
    Then I realised we can skip multiple houses and wrote the similar code as yours :
    class Solution {
    int maxi = Integer.MIN_VALUE;
    public int rob(int[] nums) {
    int[] dp = new int[nums.length];
    Arrays.fill(dp, -1);
    maxi = helper(nums, 0, dp);
    return maxi;
    }
    public int helper(int[] arr, int i, int[] dp) {
    if (i >= arr.length) return 0;
    if (dp[i] != -1) return dp[i];
    int take = arr[i] + helper(arr, i + 2, dp);
    int notTake = 0 + helper(arr, i + 1, dp);
    return dp[i] = Math.max(take, notTake);
    }
    }

  • @madhuraj4you
    @madhuraj4you Рік тому +3

    very good man!

  • @harshvardhan7368
    @harshvardhan7368 Рік тому +3

    nice explanation❤

  • @bhuppidhamii
    @bhuppidhamii 7 місяців тому +5

    MIK comes as a saviour.

  • @sudhakarsudhakar3703
    @sudhakarsudhakar3703 9 місяців тому +1

    Bro your explanations are exceptionally well crafted the finest I've come across on UA-cam

    • @codestorywithMIK
      @codestorywithMIK  9 місяців тому

      Means a lot to me.
      Thank you so much 😇❤️🙏

  • @souravjoshi2293
    @souravjoshi2293 Рік тому +3

    That's a masterpiece

  • @philosphize
    @philosphize Рік тому +2

    Thank you. Explained well

  • @nagmakhan672
    @nagmakhan672 Рік тому +2

    Cleanest explanation😊

  • @oqant0424
    @oqant0424 7 місяців тому

    solved using recursion and memoization.............came here for bottom up...and u taught it so so well🔥🔥

  • @pratibhaverma2063
    @pratibhaverma2063 7 місяців тому +2

    i love your way of explanation and solutions.SO neat and clean.helps a lot. thank you.

  • @subhrajyotisen7218
    @subhrajyotisen7218 4 місяці тому +1

    Thanks to your explanations from the previous videos, I was able to solve this before listening to your explanation :)

  • @32bhogawaraniketanil82
    @32bhogawaraniketanil82 7 місяців тому +1

    I really enjoy your explanations 🤩 The best explanation videos on internet ♥♥

  • @gauravbanerjee2898
    @gauravbanerjee2898 7 місяців тому +1

    Cold mornings🥶
    MIK bhaiya ka POTD wala video ❤
    Aur 1 cup chai ☕
    Is all you need 😁
    Thanks a lot bhaiya fr the super intuitive explanation❣

    • @codestorywithMIK
      @codestorywithMIK  7 місяців тому +2

      1 cup chai in the winter morning . What a perfect timing ❤️❤️😇
      Hope we all meet soon and have a cup of tea together and do some gup shup 🙌🙏
      Will soon plan with everyone.

    • @gauravbanerjee2898
      @gauravbanerjee2898 7 місяців тому

      @@codestorywithMIK Yes for sure a meetup will be great 🤩

  • @abhayroadlines528
    @abhayroadlines528 7 місяців тому +1

    you've got the best explanation skills❤. Thank you so much to be here on youtube.

  • @YashSinghal
    @YashSinghal Рік тому +2

    just amazing

  • @parthbhatti4151
    @parthbhatti4151 10 місяців тому +2

    Great Explanation

  • @codeyoga
    @codeyoga 7 місяців тому

    Awesome explanation for recursion + memo. In bottom up, the indexing was confusing w.r.t 2 diagrams of array (dusrava ghar and ekva ghar)

  • @theOmKumar
    @theOmKumar 7 місяців тому +1

    I was able to solve it by all 3 methods thanks to your earlier videos!!

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 Місяць тому +1

    NICE SUPER EXCELLENT MOTIVATED

  • @souravkrsingh4226
    @souravkrsingh4226 7 місяців тому +2

    Best explanation

  • @mind-010
    @mind-010 18 днів тому

    Bhaiya, what's the reason to have a array size of "n + 1" in bottom-up approach??
    How can we find whether to have a "n" sized array or "n + 1" sized array ?
    By the way, very well explained ❤

  • @joydeep-halder
    @joydeep-halder 7 місяців тому +1

    Explanation Level 🔥🔥🔥🔥

  • @VikashShukla07
    @VikashShukla07 Рік тому +1

    god level explaination.....🙌🙌🙌Thankyou so much sir

  • @priyanshukumar1020
    @priyanshukumar1020 6 місяців тому +2

    Awesome explanation

  • @aarzoo2302
    @aarzoo2302 7 місяців тому +1

    i'm crying literalllyy ; _ ; you are too gooodddddddd

  • @gocrazy6177
    @gocrazy6177 2 місяці тому +1

    DSA DP series ❌ DP Web Series ✅

  • @user-ub2is4rs4x
    @user-ub2is4rs4x 7 місяців тому

    This channel is a Gem 💎

  • @sameerswankar
    @sameerswankar 6 місяців тому

    The man, the myth, the legend !!!

  • @hydrocy.9165
    @hydrocy.9165 7 днів тому

    28:17 steal sjhould be nums[i] + nums[i-2], doesn't i-1 mean adjacent

  • @SuyashTalks
    @SuyashTalks 8 місяців тому +1

    Thanks !!

  • @KishoreKumar-rh2tw
    @KishoreKumar-rh2tw 11 місяців тому +1

    You the best!!

  • @bhuppidhamii
    @bhuppidhamii 7 місяців тому +1

    best explanation!!!!

  • @rajchinagundi7498
    @rajchinagundi7498 9 днів тому

    Hey @codestorywithMIK I was able to define the state for this problem, but then I wasn't able to come up with pick t[i-2]+current or just pick t[i-1] . I want to be able to develop that ability how do I do that?

  • @aws_handles
    @aws_handles 7 місяців тому

    You are amazing bro ❤❤❤

  • @shivamtomar9658
    @shivamtomar9658 11 місяців тому +1

    Thankyou bhaiya❤

  • @ugcwithaddi
    @ugcwithaddi Рік тому

    The best and cleanest ❤

  • @JJ-tp2dd
    @JJ-tp2dd Рік тому +1

    Java Implementation:
    Rec+Memo
    class Solution {
    private int solve(int[] nums, int[] t, int idx) {

    if(idx >= nums.length)
    return 0;

    if(t[idx] != -1)
    return t[idx];

    int take = nums[idx] + solve(nums,t,idx+2);
    int notTake = solve(nums,t,idx+1);

    return t[idx] = Math.max(take, notTake);
    }
    public int rob(int[] nums) {
    int[] t = new int[nums.length+1];
    Arrays.fill(t,-1);
    return solve(nums,t,0);
    }
    }
    Bottom Up:
    class Solution {
    public int rob(int[] nums) {
    int n = nums.length;
    int[] t = new int[n+1];

    //t[i] --> max profit obtained till house i
    t[0]=0;
    t[1]=nums[0];

    for(int i=2; i

  • @tusharnanda3885
    @tusharnanda3885 7 місяців тому +1

    done

  • @shanayagupta4002
    @shanayagupta4002 Рік тому +2

    sir what will be the time complexity of the memiozation approach ?

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      In that case, it will be O(n) because we wont visit any stare more than once.

    • @shanayagupta4002
      @shanayagupta4002 Рік тому +1

      @@codestorywithMIK okay got it sir, Thank you!

  • @humanity7880
    @humanity7880 Рік тому +1

    understood! Thank you

  • @Momentsofmagic28
    @Momentsofmagic28 7 місяців тому

    you the best

  • @akshanshsharma6025
    @akshanshsharma6025 9 місяців тому

    bro a humble request can you please upload a video of house robber III(337)on leetcode

  • @035-harshitsingh7
    @035-harshitsingh7 Рік тому +1

    Bhaiya ek basic sa doubt hai ki base case is something the largest or smallest valid case of a problem so iss case me agar i>=n hogaya tab to ghar hi exhaust ho jayega to wo ek valid case kaise hua?

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Are you referring to line-5 at 15:02 ?

    • @035-harshitsingh7
      @035-harshitsingh7 Рік тому +1

      Yes bhaiya...sorry to tag time lapse

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      @035-harshitsingh7 No worries.
      Yes it’s an invalid case that’s why we return 0 in that case

  • @oqant0424
    @oqant0424 7 місяців тому

    POTD DONE [21.1.24] ✅✅

  • @nitin_02_2
    @nitin_02_2 Місяць тому

    sir cant we start from last index

  • @anjumarakhan8647
    @anjumarakhan8647 3 місяці тому +1

    Here after Aditya verma playlist

  • @jagadeeshp1163
    @jagadeeshp1163 Рік тому +1

    in pick no pick what ever the code is for no pick or skip is it always (i+1) or it varies if varies please let me know an example where it various

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Always remember that, nopick means we are excluding/ignoring/not-taking that index and hence it will always be i+1
      BUT,
      “pick” can be tricky.
      It can be i or i+1 or start from 0 again etc

    • @jagadeeshp1163
      @jagadeeshp1163 Рік тому +1

      Thanks alot mike you clear my doubts immediately even a paid tutor takes times
      Thanks alot

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      @jagadeeshp1163 Happy to help 😇❤️🙏

  • @akarshsachan21
    @akarshsachan21 Рік тому

    why memset why not any other data structure?

  • @Shivam_7488
    @Shivam_7488 7 місяців тому

    [2,1,1,2] ispe recursion tree draw krke try kiya tha anybody? ans 4 aana chaiye but 3 h aara diagram se

    • @souravjoshi2293
      @souravjoshi2293 7 місяців тому +1

      rob({2, 1, 1, 2})
      / \
      rob({1, 1, 2}) rob({1, 2})
      / \ / \
      rob({2, 2}) rob({2}) rob({}) rob({})
      / \
      rob({}) rob({})
      I think tree diagram se bhai 4 aa Raha maximum. see the right side tree.
      We took index= 0 (amount = 2)
      And then we are left with {1, 2}
      Now, we skip I and steal 2 (index 3)
      Total = 4

  • @bitsonymous5955
    @bitsonymous5955 5 місяців тому

    31 mar 2024