DP 6. House Robber 2 | 1D DP | DP on Subsequences

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024
  • Lecture Notes/C++/Java Codes: takeuforward.o...
    Problem Link: bit.ly/3F6q83P
    Pre-req for this Series: • Re 1. Introduction to ...
    Make sure to join our telegram group for discussions: linktr.ee/take...
    Full Playlist: • Striver's Dynamic Prog...
    In this video, we have discussed how to solve the House Robber 2 problem, this is a slight variation of the previous problem that we solved in Lecture 5 of DP series.
    If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.o...
    You can also get in touch with me at my social handles: linktr.ee/take...

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

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

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

  • @priya____19
    @priya____19 7 หลายเดือนก่อน +58

    I am watching this in 2024 even now u are the best and no other video like this . Thanks you so much

  • @PirateHunter335
    @PirateHunter335 11 หลายเดือนก่อน +72

    this is called confidence, even the wrong answer seems accepted!!

    • @user-cl3un2qx7s
      @user-cl3un2qx7s 8 หลายเดือนก่อน +4

      just change int to long long int

  • @VikasGupta-ok9lh
    @VikasGupta-ok9lh ปีที่แล้ว +16

    While reading the question the same approach came to my mind and i was really happy when u explained the same approach

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

    We can also send indices as f(0,n-2) & f(1,n-1) to the function and compute. Wont need the temp array in that case.

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

      gives TLE

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

      @@abhijitroy1958
      class Solution
      {
      public:
      int robOriginal(vector& nums, int start, int end)
      {
      int last = 0, last_last = 0, res = 0;

      for(int i = start; i < end; ++i)
      {
      res = max(last_last + nums[i], last);
      last_last = last;
      last = res;
      }
      return res;
      }
      int rob(vector& nums)
      {
      if(nums.empty()) return 0;
      if(nums.size() == 1) return nums[0];
      int n = nums.size();
      return max(robOriginal(nums, 0, n-1), robOriginal(nums, 1 , n));
      }
      };

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

      That's recursion and not optimised

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

      I have done same thing. Successfully submitted.

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

    That click in your brain at 4:55. Like damn striver. You are just too good in teaching

  • @NaveenKumar-zp4sy
    @NaveenKumar-zp4sy 2 ปีที่แล้ว +46

    Understood :) After listening to logic without further viewing code, I am abe to code the logic on own!! Nice series bro 🔥.... Tons of love 🥰

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

    understooddddd , Woww this man has so much knowledge , he deserves the google job and more , Keep going bro , you have a long way to go , all the best for tuf+

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

    Timestamps:
    Intro 00:00
    Problem Statement 00:38
    Intuition 02:22
    Coding 06:12
    Also, understood

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

    Immense respect for the hardwork and dedication you put into teaching us. the last line you said that you make these dsa videos at night post working hours gave me so much motivation. Youre one of the biggest inspirations striver.
    we, your students, are proud of you.

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

    Understood 🔥🔥✨
    A great playlist, great explanations and a great teacher !!!!
    What else anyone need ?

  • @user-vn4tt9gg3j
    @user-vn4tt9gg3j ปีที่แล้ว +3

    Absolutely the best!! I am able to solve questions on my own too because I am able to think in the right direction now. Thank you Stiver

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

    In the article it is mentioned that the space required is O(1) but the solution uses O(N) extra space for creating two additional arrays.
    Here is the true O(1) space solution
    long long int houseRobberUtil(vector& valueInHouse, bool check) //check=true if we pick the first 1st element and false if we don't pick the first element.
    {
    long long int prev1,prev2;
    int i,n;
    if(check)
    {
    prev2=valueInHouse[0];
    i=1;
    n=valueInHouse.size();
    n-=1;
    }
    else
    {
    prev2=valueInHouse[1];
    i=2;
    n=valueInHouse.size();
    }
    prev1=prev2;
    long long int ans=0;
    while(i1)
    pick+=prev2;
    }
    else
    {
    if(i>2)
    pick+=prev2;
    }
    long long int notPick=prev1;
    ans=max(pick,notPick);
    prev2=prev1;
    prev1=ans;
    i++;
    }
    return prev1;
    }
    long long int houseRobber(vector& valueInHouse)
    {
    if(valueInHouse.size()==1)
    return valueInHouse[0];
    long long int inc=houseRobberUtil(valueInHouse,true);
    long long int exc=houseRobberUtil(valueInHouse,false);
    return max(inc,exc);
    }

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

    Understood, thank you sir, the logic of leaving the first element and applying maxSumSubseq on the remaining and leaving the last element and considering the starting n-1 element is amazing.

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

    Production quality code wow this is what even interviewers expect ,thnks for such a good quality of code as well 💯🔥

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

    I paused the video and thought of this, and I cracked the intuition, also I think there's no need to take two extra vectors, temp1 and temp2, it could be done without that as well. thank you for the video, this is becoming addictive.

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

      We can include start and end indices in function
      Func(nums,start,end):
      n= nums.size()
      ---------
      For(i= 0,i

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

    Makes video at late night for free. Striver is the definition of an educator🗿.

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

    Tons of love brother, your dedication is what makes you different from others!

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

    Because of your lucid explanations, i've been able to do this one directly with space optimization in two traversals🙏🙏

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

      you can do it in 1 traversal, using 4 variables

  • @kellysmith-t9u
    @kellysmith-t9u หลายเดือนก่อน

    Thanks a ton for making such educative videos and also for taking the time to correct the mistakes(if any) in the video!

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

    understood bhaiya❤...whenever your heart is broken. don't ever forget that you are golden......enjoy....1000 !!

  • @Zomb-zj4ip
    @Zomb-zj4ip 2 หลายเดือนก่อน

    understood and imprinted in memory forever

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

    we can also do it by dp solution of lecture 5 ... just use long long in dp array and make 2 dp array
    call the function for (n-2)
    reverse the array
    again call the function for (n-2)
    max of both

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

      @@ojasahirrao3287
      according to video
      long long int maximumNonAdjacentSum(vector &v1){
      long long int n = v1.size();
      long long int prev = v1[0];
      long long int prev2 = 0;
      for(int i=1;i1) take+=prev2;
      long long int notTake = prev;
      long long int current = max(take,notTake);
      prev2 = prev;
      prev = current;
      }
      return prev;
      }
      long long int houseRobber(vector& nums){
      int n = nums.size();
      if(n==1) return nums[0];
      vector s1,s2;
      for(int i=0;i

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

      @@ojasahirrao3287
      from DP lecture 5
      reversing the array
      #include
      long long int solve(long long int i,vector &v1,vector &dp){
      if(i == 0) return v1[i];
      if(i < 0) return 0;
      if(dp[i]!=-1) return dp[i];

      long long int left = INT_MIN;
      long long int right = INT_MIN;
      left = solve(i-2,v1,dp)+v1[i];
      right = solve(i-1,v1,dp);
      return dp[i] = max(left,right);
      }
      long long int houseRobber(vector& valueInHouse){
      long long int n = valueInHouse.size();
      vector dp(n,-1),dp1(n,-1);

      if(n==1) return valueInHouse[n-1];

      long long int ans1 = solve(n-2,valueInHouse,dp);
      reverse(valueInHouse.begin(),valueInHouse.end());
      long long int ans2 = solve(n-2,valueInHouse,dp1);

      return max(ans1,ans2);
      }

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

      @@champion5946 Thank you for the code. I solved it using dp(memoisation). I forgot to make two dp arrays.

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

      @@ojasahirrao3287 ✌🏻np

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

      @@leetcoder1159 simp

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

    Amazing Playlist Striver

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

    Question 00:01
    Intuition 02:20
    Code 06:08

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

    I was able to code the whole ans right after you explained it. Then I watched it thereafter. Thanks Striver.

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

    Using *"low"* & *"high"* indices instead of creating new vector
    int func(vector &A, int low, int high)
    {
    int prev2 = 0, prev1 = A[low];
    for(int i=low + 1; i 1) pick += prev2;
    int notPick = 0 + prev1;
    int curr_i = max(pick, notPick);
    prev2 = prev1;
    prev1 = curr_i;
    }
    return prev1;
    }
    int rob(vector& A)
    {
    int n = A.size();
    if(n == 1)
    return A[0];
    int m1 = func(A, 0, n-2); // Exclude last elem.
    int m2 = func(A, 1, n-1); // Exclude first elem.
    return max(m1, m2);
    }

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

    not only did i understand, but i was also able to solve this problem myself, just by watching your previous tutorials. keep up with the good work man

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

    /*with space optimization (no need vector temp1 and temp2)*/
    int robhelp(int start,int end,vector &nums) {
    int prev1=nums[start];
    int prev2=0,curr;
    int fs,ss;
    if(end-start==1)
    return nums[start];
    for(int i=start+1;istart+1)
    fs+=prev2;
    ss=prev1;
    curr=max(fs,ss);
    prev2=prev1;
    prev1=curr;
    }
    return curr;
    }
    int rob(vector& nums) {
    int n=nums.size();
    if(n==1)
    return nums[0];
    return max(robhelp(0,n-1,nums),robhelp(1,n,nums)) ;

    }

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

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

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

    Problem Link : leetcode.com/problems/house-robber-ii/
    class Solution {
    public:
    int solve(vector nums, int start, int n){
    int prev2 = 0, prev = nums[start];
    for(int i=start+1 ; i

    • @user-ie8sy7wo4m
      @user-ie8sy7wo4m ปีที่แล้ว +1

      Thanks for leetcode problem link. Appreciated 🔥

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

    "us" striver sir "us" love ur content and the value it provides

  • @AbhishekKumar-cv1dh
    @AbhishekKumar-cv1dh 10 หลายเดือนก่อน

    Loved the content, the neighbour logic followed reduction to existing problem which was simply amazzziiiing ❤‍🔥❤‍🔥❤‍🔥❤‍🔥

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

    Understood😍First channel makes me understand about algorithms such as recursion, backtracking and dp.

  • @Stranger-wr4dg
    @Stranger-wr4dg ปีที่แล้ว

    You have made topics like dp easier to understand. Hats off man.

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

    Understood , thanx bro you explained the problem as simple as possible

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

    understood bro ,very nice teching bro i liked very much many of them are following your teaching and many suggested about you channel to learn programming thanks bro

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

    Striver your videos are really helpful. Thankyou so much for such valuable and top-notch content❤

  • @SelvaArumugam-xq5mf
    @SelvaArumugam-xq5mf 9 หลายเดือนก่อน

    Understood , Its a great logic the way you explain it makes it super easy💯

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

    Understood man!!!!
    Thanks a lot !!

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

    Amazing video.
    Just a comment regarding the extra space. We could have passed the original array instead of making two separate ones along with start and end indexes! Cheers!

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

    interseting to see how simply you broke down the logic

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

    7/57 done!!! Understood!

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

    Sir, we can make two functions by running loop from 0 to n-1 and in another function from 1 to n and call these functions to get answer . I think by this way we don't have to take extra arrays.... Thank you ❤

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

    what a logic .......thanku striver ......understood

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

    Understood Striver Thankyou for this wonderful lecture

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

    Brilliant, One point we can save more space by not even creating two separate input array instead, Add one parameter in maximumNonAdjacentSum method as starting index and send it as 0 in one call and 1 in another call and play around that.

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

    understood, thanks for this amazing series...

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

    with each day ,i am becoming master in dsa bcoz of u,
    better understood

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

    Clear and Best Explanation!!!❤❤

  • @AniketKumar-hf2bo
    @AniketKumar-hf2bo 22 วันที่ผ่านมา

    Understood completely !!❤❤❤❤

  • @snehadasb.tech-cse8446
    @snehadasb.tech-cse8446 10 หลายเดือนก่อน

    Understood. Thanks a lot bhaiya for such a great playlist.

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

    class Solution {
    public int rob(int[] nums) {
    int n = nums.length;
    if (n == 1) {
    return nums[0];
    }
    // Variables to keep track of the maximum amount of money at each step
    int prev1 = 0; // Maximum amount when excluding the first house
    int prev2 = 0; // Maximum amount when excluding the last house
    for (int i = 0; i < n - 1; i++) {
    int temp = prev1;
    prev1 = Math.max(prev2 + nums[i], prev1);
    prev2 = Math.max(temp, prev2);
    }
    int max1 = prev1;
    prev1 = 0;
    prev2 = 0;
    for (int i = 1; i < n; i++) {
    int temp = prev1;
    prev1 = Math.max(prev2 + nums[i], prev1);
    prev2 = Math.max(temp, prev2);
    }
    int max2 = prev1;
    return Math.max(max1, max2);
    }
    } @takeUforward have look on this solution

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

    Dada the way you teach...it actually feels ki yes I am learning the concepts well...thnx a lot for this series

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

    Understood , Thanks Striver

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp ปีที่แล้ว +13

    Not used any Extra Space, All 4 codes are below. All thanks to Striver
    Brute Force Approach:-
    long long int recursive(int ind, vector&a,int end){
    if(ind

  • @SS-pf8zm
    @SS-pf8zm ปีที่แล้ว +1

    Striver, "Understood!"💖💖💖💖

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

    UNDERSTOOD

  • @EknathMali-us2nv
    @EknathMali-us2nv ปีที่แล้ว +1

    at a time 2:53 you are making video, this gives us more strength to learn & watch more videos of yours❤❤

  • @abdussalam-4836
    @abdussalam-4836 5 วันที่ผ่านมา

    Nothing is better than striver .

  • @user-ye5zq6hr7r
    @user-ye5zq6hr7r 6 หลายเดือนก่อน

    Highly motivated sir...if you can make efforts to record the video at 2am the least i could for myself is watch and learn...us.

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

    Understood ❤

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

    US :) Appreciate your efforts
    PS:- Commenting today when I have no job.. Will come back and comment again when I will get a new job. Till then, Thanks in advance Striver

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

    THANK YOU STRIVERRRRR!!!!!!!!!!1
    UNDERSTOODDD!!!!!

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

    Understood...Completed 6/56

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

    Understood very well

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

    if we ignore the last element then it means from 0 to 2nd last element maximum sum.It doesnt mean that you are taking one for sure, your subsequence might not contain first element in this case we can add the last element as well .....isnt it??

  • @LokeshSharma-hm5jz
    @LokeshSharma-hm5jz ปีที่แล้ว

    I regret why I cant follow you earlier. Thanks a lot.

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

    Understood Striver🙌

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

    understood ❤ You are amazing man , love u so muchh 😍

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

    Python solution of prob mentioned in this video
    def max_theft(a):
    n = len(a)
    prev1 = a[0]
    prev2 = 0
    for i in range(1, n):
    pick = a[i]
    if(i>1):
    pick+= prev2
    not_pick = 0 + prev1
    curr = max(pick, not_pick)
    prev2 = prev1
    prev1 = curr
    return prev1
    a = [2,3,2]
    if(len(a)==1):
    print(a[0])
    print(max(max_theft(a[1:]), max_theft(a[:-2])))

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

    wonderful series bro, i loved this keep doing good work, these things come back

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

    Another similar way: We can choose to take A[0] or not in solution: ```return max(helper(arr, 1, n), helper(arr, 2, n-1) + A[0]);```

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

    Understood 😄

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

    Understood

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

    Is this logic right even for negative integers?

  • @Mr.ShortBeast2.O
    @Mr.ShortBeast2.O 2 หลายเดือนก่อน

    Superb Content!!!

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

    "understood" bhaiya i had a question like we are taking extra array for storing temp and temp1 so waha to space ja raha hai

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

    understood

  • @user-mq4fu9yh4t
    @user-mq4fu9yh4t ปีที่แล้ว

    clearly undershood ..i appreciate for your work sir;

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

    Understood! made especially easy after watching the last video!

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

    Pure genius

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

    NICE SUPER EXCELLENT MOTIVATED

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

    okayyyy this was amazing!!! day1 and 6 videos done!!!

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

    great explanation

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

    Understood Awesome explanations!!
    A request to add Interleaving Strings and Catalan number problems

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

    I am proud to have You 🥰 in my Life.

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

    Understood and awesome as usual

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

    "UNDERSTOOD!!"

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

    Understood !!!!

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

    Understood Understood Understood!!! Best Sir ever🥰🥰🥰

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

    Understood very easily😊😊

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

    US bhaiya, awesome intuition!

  • @BG-lj6fw
    @BG-lj6fw ปีที่แล้ว

    Understood. salute to ur hardword. Inspiring.

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

    Thank You

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

    Thanks for great explanation.

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

    UNDERSTOODD!!!

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

    Day 2

  • @VinayKumar-xs6el
    @VinayKumar-xs6el 16 วันที่ผ่านมา

    guys can use indices instead taking extra 2 arrays

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

    Wouldn't the first and last indices be included if the values are something like {10, 2, 3, 4, 20}? I might be missing something out here, but the same code for DP 5 (without any logical changes) worked for this problem for me.