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

Sdílet
Vložit
  • čas přidán 13. 01. 2022
  • Lecture Notes/C++/Java Codes: takeuforward.org/data-structu...
    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/takeUforward
    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.org/interviews/s...
    You can also get in touch with me at my social handles: linktr.ee/takeUforward

Komentáře • 1,4K

  • @takeUforward
    @takeUforward  Před 2 lety +193

    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 Před 6 měsíci +40

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

  • @PirateHunter335
    @PirateHunter335 Před 9 měsíci +54

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

  • @VikasGupta-ok9lh
    @VikasGupta-ok9lh Před rokem +13

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

  • @user-vn4tt9gg3j
    @user-vn4tt9gg3j Před rokem +2

    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

  • @manandhyani7129
    @manandhyani7129 Před 2 lety +65

    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 Před 2 lety +1

      gives TLE

    • @shaunakdeshpande7295
      @shaunakdeshpande7295 Před 2 lety +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 Před rokem

      That's recursion and not optimised

    • @kvchahar10
      @kvchahar10 Před rokem +1

      I have done same thing. Successfully submitted.

  • @Stranger-wr4dg
    @Stranger-wr4dg Před rokem

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

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

    I devised the solution without going through the full video and coded and submitted using same logic from L7!! Great teaching Striver!!

  • @rohit1770
    @rohit1770 Před 2 lety +8

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

  • @Zunan263
    @Zunan263 Před 2 lety +12

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

  • @asmitraj4740
    @asmitraj4740 Před 6 měsíci

    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.

  • @vudangkhoa2906
    @vudangkhoa2906 Před rokem

    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

  • @NaveenKumar-zp4sy
    @NaveenKumar-zp4sy Před 2 lety +45

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

  • @sujan_kumar_mitra
    @sujan_kumar_mitra Před 2 lety +39

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

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

    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.

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

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

  • @nopecharon
    @nopecharon Před rokem +4

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

  • @deepakojha3216
    @deepakojha3216 Před 2 lety +4

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

  • @nentangkienthuc6276
    @nentangkienthuc6276 Před rokem

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

  • @mihirjain3932
    @mihirjain3932 Před rokem

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

  • @pratyakshsinha6292
    @pratyakshsinha6292 Před rokem +5

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

  • @sommayghosh4617
    @sommayghosh4617 Před 2 lety +15

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

    • @zeroanims4113
      @zeroanims4113 Před rokem +2

      you can do it in 1 traversal, using 4 variables

  • @roshangupta8161
    @roshangupta8161 Před rokem +2

    Clear and Best Explanation!!!❤❤

  • @selva140
    @selva140 Před 10 měsíci +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

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

    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);
    }

  • @kuldeepkushwaha9351
    @kuldeepkushwaha9351 Před 2 lety +11

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

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

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

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

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

  • @champion5946
    @champion5946 Před 2 lety +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 Před 2 lety +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 Před 2 lety +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 Před 2 lety

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

    • @champion5946
      @champion5946 Před 2 lety

      @@ojasahirrao3287 ✌🏻np

    • @codingwithanonymous890
      @codingwithanonymous890 Před 2 lety

      @@leetcoder1159 simp

  • @ABCD-bh6ci
    @ABCD-bh6ci Před 7 měsíci +1

    Understood man!!!!
    Thanks a lot !!

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

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

  • @datkumar1024
    @datkumar1024 Před 2 lety +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);
    }

  • @sumit1797
    @sumit1797 Před 2 lety +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 Před rokem +1

      Thanks for leetcode problem link. Appreciated 🔥

  • @anshpradhan1954
    @anshpradhan1954 Před 7 dny

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

  • @SelvaArumugam-xq5mf
    @SelvaArumugam-xq5mf Před 7 měsíci

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

  • @Solar_unfiltered
    @Solar_unfiltered Před 2 lety +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)) ;

    }

  • @berserker556
    @berserker556 Před měsícem

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

  • @Zomb-zj4ip
    @Zomb-zj4ip Před měsícem

    understood and imprinted in memory forever

  • @raghavmanish24
    @raghavmanish24 Před měsícem

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

  • @user-el6hv6em7m
    @user-el6hv6em7m Před 12 dny

    understood, thanks for this amazing series...

  • @AbhishekKumar-cv1dh
    @AbhishekKumar-cv1dh Před 9 měsíci

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

  • @snehadasb.tech-cse8446
    @snehadasb.tech-cse8446 Před 8 měsíci

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

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

    Amazing Playlist Striver

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

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

  • @shashankgarg8128
    @shashankgarg8128 Před rokem

    Understood , and thanks for making such a great content on YT.

  • @user-mq4fu9yh4t
    @user-mq4fu9yh4t Před rokem

    clearly undershood ..i appreciate for your work sir;

  • @jayeshnayak9759
    @jayeshnayak9759 Před rokem

    Amazing series !!

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

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

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

    understood. The best dp series

  • @Mr.ShortBeast2.O
    @Mr.ShortBeast2.O Před 21 dnem

    Superb Content!!!

  • @ritikshandilya7075
    @ritikshandilya7075 Před měsícem

    Understood , Thanks Striver

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

    Thanks for great explanation.

  • @souhardyasaha6072
    @souhardyasaha6072 Před rokem

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

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

    understood this. thank you sir

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

    7/57 done!!! Understood!

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

    understood and thanks a lot

  • @tasneemayham974
    @tasneemayham974 Před rokem

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

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

    Understood very easily😊😊

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 Před rokem +1

    Understood and awesome as usual

  • @sandeepparimi5316
    @sandeepparimi5316 Před rokem

    Superbly explained bro, I understood it clear👌👌

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

    great explanation

  • @prasannasippa5962
    @prasannasippa5962 Před rokem

    Thank you so much striver !!!

  • @sajalgupta3888
    @sajalgupta3888 Před rokem +1

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

  • @vakhariyajay2224
    @vakhariyajay2224 Před rokem

    Thank you very much. You are a genius.

  • @user-bt6mh9ez3u
    @user-bt6mh9ez3u Před 20 dny

    Understood very well

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp Před rokem +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

  • @hrushikesh-1914
    @hrushikesh-1914 Před 10 měsíci

    Understood. Thank you sir

  • @BG-lj6fw
    @BG-lj6fw Před rokem

    Understood. salute to ur hardword. Inspiring.

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

    Understood Striver Thankyou for this wonderful lecture

  • @subhadipmaity3253
    @subhadipmaity3253 Před rokem

    Understood !! Awesome Technique

  • @b78yashodhanbhatawdekar58

    Understood! made especially easy after watching the last video!

  • @aps8874
    @aps8874 Před měsícem

    Thank you so much :)

  • @a8hi83
    @a8hi83 Před rokem

    Amazing Explanation!!

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

    Good stuff Striver!

  • @vinothveeriyaperumal6305

    Understood sir!!! your way of teaching is so damn amazing😀

  • @sudhanshuraj8080
    @sudhanshuraj8080 Před 6 měsíci

    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 Před 6 měsíci +1

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

  • @harshkanani6605
    @harshkanani6605 Před rokem

    understood man!! Thank you so much

  • @bravekumar507
    @bravekumar507 Před rokem

    Understood amazing !!!

  • @soorajkumar3049
    @soorajkumar3049 Před rokem

    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.

  • @HIMANSHU-u6y
    @HIMANSHU-u6y Před 11 dny

    Amazing❤

  • @PranjalAgarwal-q4q
    @PranjalAgarwal-q4q Před 26 dny

    thanks striver!

  • @user-yp7lm9em2n
    @user-yp7lm9em2n Před 10 měsíci

    UNDERSTAND VERY WELL SIR

  • @stevewilsonraj
    @stevewilsonraj Před rokem

    great explanation !!

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

    understood bhaiya

  • @yashrastogi4994
    @yashrastogi4994 Před rokem

    really great content !!

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

    Understood ❤

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

    understood bhaiya this is day 3 and really loving it

  • @sambhramshetty9385
    @sambhramshetty9385 Před dnem

    Thank U Sir!!

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

    Understood 💯

  • @ChutHunter
    @ChutHunter Před 6 měsíci

    understood ❤

  • @Missbani9
    @Missbani9 Před 2 lety

    Understood! Amazing content like always :)

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

    great work man.good content

  • @radharamamohanakunnattur3035

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

  • @yathinrasineni9089
    @yathinrasineni9089 Před rokem

    Understood! Thank you!

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

    Understood🔥

  • @thanushreddy1020
    @thanushreddy1020 Před 9 dny

    understood
    🔥🔥🔥

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

    Understood 😊

  • @ahipsharma
    @ahipsharma Před rokem

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

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

    understood👍

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

    Understood🎉