House Robber - Leetcode 198 - Python Dynamic Programming

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ค. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🐦 Twitter: / neetcode1
    🥷 Discord: / discord
    🐮 Support the channel: / neetcode
    Twitter: / neetcode1
    Discord: / discord
    💡 CODING SOLUTIONS: • Coding Interview Solut...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    🌲 TREE PLAYLIST: • Invert Binary Tree - D...
    💡 GRAPH PLAYLIST: • Course Schedule - Grap...
    💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
    💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
    Problem Link: neetcode.io/problems/house-ro...
    0:00 Conceptual
    8:03 Coding optimal solution
    #python #neetcode #leetcode
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    🚀 neetcode.io/ - A better way to prepare for Coding Interviews

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

      Good playlist 👍

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

      I solved this problem in n^2 time complexity and leetcode gave me 100% beat, i thought it was the only solution (finding the max from i+2 to n) and when i open youtube it gave me this video as recommendation.😂😂😂😂

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

    I robbed my entire neighborhood thanks to this video.
    Posting this from jail though

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

      sorry but told you to optimize first

    • @eobardthawne6903
      @eobardthawne6903 11 หลายเดือนก่อน +7

      You didn't optimize property bro

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

      Wow, can’t believe you robbed from two adjacent houses

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

    Thanks to leetcode i've given up working as a software engineer, I am now a professional house robber.

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

    This video really helped a lot! Thanks to you, I've gotten away with multiple house robberies.

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

      💀

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

      Had us in the first half, not gonna lie

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

      🤣🤣🤣🤣

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

      😂😂😂😂

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

    I like how they upgraded this to medium haha

  • @AshishSarin1
    @AshishSarin1 ปีที่แล้ว +122

    Thanks!
    I submitted correct code on leetcode just by watching conceptual part and without even seeing the coding part and later when I checked my code was almost identical to what you wrote. Excellent explanation. You are doing a really great work here

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

      what was your solution if you don't mind me asking?

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

      class Solution {
      public:
      int rob(vector& nums) {
      int n = nums.size();
      vector dp (n, 0);
      dp[0] = nums[0];
      for (int i = 1; i < n; i++) {
      dp[i] = max(dp[i - 1], nums[i]);
      if (i > 1) dp[i] = max(dp[i], nums[i] + dp[i - 2]);
      }
      return dp[n - 1];
      }
      };@@sonicspider49

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

      you inspired me bro.

  • @DavidDLee
    @DavidDLee ปีที่แล้ว +25

    The video section until 5:05 seems to show one way to solve this, while 5:05-on shows a similar, but actual way the code you wrote solves it.
    When viewing the first time, it seems puzzling why you seem to go back to solving it from scratch, after getting important insights. While the 5:05 section explains why this will be the maximum, there's some gap and the two sections seem to be somewhat parallel in first view.
    It would have helped to tie them together better.

  • @X_platform
    @X_platform 3 หลายเดือนก่อน +15

    The recurrence relationship of dynamic programming is based on "previous" optimal substructure.
    Instead of defining it:
    rob[n] = max( arr[n] + rob[2:n], rob[1:n] ) @4:40
    It should be:
    rob[n] = max( arr[n] + rob[n-2], rob[n-1] ).
    Hope this helps people who came across this post.

    • @rc-zl9dp
      @rc-zl9dp 7 วันที่ผ่านมา

      It clicked for me after reading this comment, thanks!

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

    For people confused this is a space optimised dp solution. Similar to solving fibonacci iteratively. If you compare to solving fiboannci with a memo table, it will be more similar and more intuitive to further dp problems.

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

    Thanks for listening, and adding a dp question! Though I had solved this before, learnt a ton from this one! Thank you @NeetCode.

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

    This is helpful. DP is so difficult for me; I've watched the Free Code Camp video on it and practiced dozens of problems, but I just have some sort of mental block when it comes to this topic. Thank you for the video!

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

      you are not alone!! xD

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

      Look up 'mental block' in the dictionary, you'll find my face next to it.

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

      same!!

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

      Wow me too! TT^TT im not alone on this DP mental block

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

      @@kimberlygovea8591 did you get better?

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

    I love how you explain the problem with calm and clear english. Thank you very much, I didn't think I would understand how to solve this problem. But after watching your video I feel like dynamic programming is easier than I thought. Thanks a lot man.

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

    you should explain that we return rob2 because rob2 is actually temp right now, which is essentially the max we can get for the last house.

  • @edsalazar4590
    @edsalazar4590 ปีที่แล้ว +6

    Good to know there's an actual algorithm for robbing houses!

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

    Your explanation is the clearest explanation that I had ever found! thank you so much!

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

    Thank you bro, your video really helped me understand how i should approach dynamic programming problems. Thank you so much again!!!

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

    Bottom-up approach:
    class Solution:
    def rob(self, nums: List[int]) -> int:
    nums.append(0)
    for i in range(len(nums) - 3, -1, -1):
    nums[i] = max(nums[i] + nums[i + 2], nums[i + 1])
    return max(nums[0], nums[1])

    • @bizman7485
      @bizman7485 25 วันที่ผ่านมา

      That’s how I did it. I thought it was pretty optimized. But the way Neet did it has an O(1) space complexity

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

    Thanks! Leetcode changed this problem to medium difficulty 😃

  • @kaka83185
    @kaka83185 16 วันที่ผ่านมา +1

    Thanks for the explanation. Here is an alternative solution that can be easier to understand :
    def rob(self, nums: List[int]) -> int:
    if not nums :
    return 0
    if len(nums) == 1:
    return nums[0]
    dp = [0] * len(nums)
    dp[0] = nums[0]
    dp[1] = max(nums[0], nums[1])
    for i in range(2,len(nums)):
    dp[i] = max(dp[i-1],nums[i]+dp[i-2])
    return dp[-1]

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

    Such great explanation! Thanks mate and keep going

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

    Every single time I look for any solution, I try to search your channel first that, have you uploaded its solution. Thanks for amazing solutions ♥

  • @vyt1706
    @vyt1706 11 วันที่ผ่านมา

    your logic is so insane man, every approach you come up with my jaw drop and its really impressive how you think, it might seem exaggerating but as a newbie coder who gets stuck frequently, it is impressive what you do

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

    Best man on the earth, clear and crisp solutions. Thankyou so much.🙌🙌👌

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

    What about making each week a video about the leetcode weekly contest hard question? I always have problems to solve this and maybe there is a niche for it, because nobody else does a clear video solution about this!

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

    I was struggling with understanding solutions in the forum, this really helped clarify how recurrence relation is working here

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

    That was cool - doing it in O(1) space.

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

    Simple clean explanation 🔥🔥... great job man....

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

    Awesome explanation! Thank you so much!

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

    Amazing. I tried memoization which also worked. But your solution's conciseness is simply wow

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

      Same. But I'm struggling to edit the recursive solution for House Robber 2

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

    Nice Explanation!
    From your conceptual explanation I came up with this solution.
    class Solution:
    def rob(self, nums: List[int]) -> int:
    length = len(nums)
    if length

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

    we can also work this backward from N-1 to 0-th index :D. denote DP[n] as the robbed amount if we start from the n-th house (including the n-th), denote DPN[n] as robbed amount if we start from at least n+1-th house (not including n-th). DP[n] = nums[n] + max(DP[n + 2], DPN[n + 2]). DPN[n] = max(DP[n + 1], DPN[n + 1]). we initialise DP[N-1] = nums[N -1], DPN[N-1] = 0 and also the N -2 ones .

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

    You are a great human being. Thanks.

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

    After learning a lot of dynamic programming problems from you.... Today I was able to solve this by myself.

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

    The solution, video, and explanation are beautiful.

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

      Thanks 😊

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

    Maybe it's just me but I think it is way more understandable without auxiliary variables and implement a bottom-up DP solution. It helped me to store the max amount of robbed in a int[] dp array and initialize array to have [nums[0], max(nums[0], nums[1]).... and then iterating through the nums array.

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

      you can even use the nums argument directly.

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

    I solved it with Recursion but this solution is smart! Thank You.

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

    can't believe you are writing with a mouse .... Appreciate your efforts dude... Awesome explanation by the way

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

    LeetCode updated problem 198 difficulty to Medium.
    Should this video be moved from Easy playlist to Medium also?
    It's kind both, isn't it?
    With DP intuition, it becomes relatively easy (after watching your explanation).

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

    It took me a while to get it... Interesting logic.

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

    The part that makes sense is choosing the first or not and defining that in terms of recursion: `max(rob(nums[1:]), nums[0] + rob(nums[2:]))` -- But the switch from a recursive approach to this rolling array is confusing. Memoization can also be done with a dictionary -- it's O(n) instead of O(1), but for whatever reason it makes more sense to me...

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

    lightbulb went off for me @ 9:30 . thank you dude

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

      Glad it was helpful!

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

    little correction, check for input [2,1,1,2]
    // Assuming all +ve values
    var rob = function(nums) {
    if(!nums) return 0;
    if(nums.length == 1 ) return nums[0];
    if(nums.length == 2) return Math.max(nums[1], nums[0]);

    let rob1 = nums[0];
    let rob2 = Math.max(nums[0], nums[1]);
    for (let i=2; i< nums.length; i++) {
    // Here we are
    let temp = Math.max(rob1 + nums[i], rob2);
    rob1 = rob2;
    rob2 = temp;
    }
    return rob2;
    };

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

    I understand what you did, but I still don't understand how I can come up with the same solution.
    Definitely need more practice.

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

    Wow I wish I saw this video before my Cisco assessment, I got a question exactly like this except the story was that you couldn’t pick two chocolates from adjacent candy jars

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

    Great solution and explanation: quite easy to grasp, but how is it related to the dynamic programming approach?

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

      This is DP coded in bottom-up approach. If top-down, then it will be using recursion

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

    Thanks a lot man!

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

    The confusing part for me was/is to understand why we need to calculate max. I think with a larger input array it could be clearer. The answer I came up with to my own question it's because the current element will become (after we move forward two times) the element that we want to check how much we could rob when we where there.

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

      Max is a built in function to get max value from two inputs

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

    This method of resolutions make me think of the concept of two pointer or even sliding windows.

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

    I am dumb, I don't get your explanation at all and it seems I'm the only one, looking at the comments.

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

      you're not the only one!

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

    you have explained this problem so well.

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

      Thanks 😊

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

      @@NeetCode so time is O(n) and space is O(1)?

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

    This is amazing, how could you come up with this ideal

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

    This is a Medium now

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

    MAN I LVOE YOUR VIDEOS!!!

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

    excellent explanation

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

    Found one problem in explanation 4:30 time rob = max(arr[0] + rob[2:n], rob1) actually you can't go to [2:n] but [2:n-2], [1:n-1] as you picked the arr[0] from the example. This problem you coded is basically optimization for dynamic arr problem. I found dp array problem easier to understand.

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

    Hello, how can we rob in house 0 &1 and ignore 3 at index 4 ? Robbing in house 0&1 means that we rob in adjacent houses right ?

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

    Pretty happy I was able to come up with the solution for this one, including using memoization and submitting on Leetcode, in like 15 min.
    Came here to see if my solution was actually optimal. Feeling a lot closer to being ready for my interviews and these videos are definitely helping.

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

    So, it’s essentially “max fibonacci” with random values :o

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

    At about 5:15, its mentioned that doing computations from the end of the array can be confusing - even I tried doing that and got it incorrect multiple times. How do you guys determine in such problems whether to start from the front or from the back? I did Min Cost Climbing Stairs just before this problem and therein I noticed that it was simpler to do the computation from the end. I'm in quite some dilemma!

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

      Got it using the other end of the array - Memoized solution :-
      class Solution {
      public int robAmount(int[] nums, int[] dp, int n) {
      if (n < 0)
      return 0;
      if (dp[n] != -1)
      return dp[n];
      dp[n] = Math.max(nums[n] + robAmount(nums, dp, n - 2), robAmount(nums, dp, n - 1));
      return dp[n];
      }
      public int rob(int[] nums) {
      int n = nums.length, dp[] = new int[n];
      Arrays.fill(dp, -1);
      return robAmount(nums, dp, n - 1);
      }
      }

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

    The way he naming variable maybe be confusing;
    in my op: the current max Rob depends on prev rob and the 2 prev
    Since the current rob depends on 2 previous rob; we only need to keep track of them only; not the entire map. And this called improved DP.
    so it could be
    loop through the array:
    #find current max
    -curr = max (n + 2prev, prev) #n + 2prev cuz of parallel constraint.
    #move the prev and 2prev up 1.
    -prev2 = prev
    -prev = curr
    finally return prev which is the curr.
    You can also use something similar to this to solve fibonacci, which doesn't need to store the entire map.

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

    I was about to give up and look at the solution. Then I found that it was once tagged as an easy problem (it's medium now). I looked at my code once again and found the solution.

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

    One thing that should be called out here is the concept of “optimal substructure”.
    The reason this works is because when we look at solving the problem at house i, if we know the optimal solution at house i-1 and i-2 then we can always find the optimal solution at house i. This is expressed as
    optimalSolution[i] = max(optimalSolution[i - 1], houseValue[i] + optimalSolution[i - 2])
    The use of the names “rob1” and “rob2” aren’t very helpful IMO to understand what we are actually storing in those variables, which is the optimal solution at the 2 prior positions.

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

    I have a question. What if we skip 2 houses from the 1st house, and than either 1 or 2 houses and so on? Why this route wouldn't bring us to a more yield?

  • @Techgether
    @Techgether 3 วันที่ผ่านมา

    my solution with top down approach dp: (I also did a step by step thought process explanation in leetcode)
    class Solution:
    def rob(self, nums: List[int]) -> int:
    if len(nums) == 1:
    return nums[0]
    res = 0
    def dp(i):
    if i < 0:
    return
    tmp1, tmp2 = 0, 0
    if i + 2 < len(nums):
    tmp1 = nums[i+2]
    if i + 3 < len(nums):
    tmp2 = nums[i+3]
    nums[i] += max(tmp1, tmp2)
    dp(i-1)
    dp(len(nums)-1)
    return max(nums[0],nums[1])

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

    brilliant!

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

    Is this solution also work for if there is negative values?

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

    So elegant!

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

    You’re a god man.

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

    If I may, from what I have understand of DP so far is that you iteratively save your result in an array/table. So I suppose that your solution at 7:02 is DP and I managed to understand it and implement a solution. However when you only use you variables rob1 and rob2 I get lost because for me this is no DP anymore...
    Am I wrong?

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

      DP can be stored in an array that is the length of what you know the final result set should be
      BUT
      To save space you're encouraged to only use what you need, of course, and in many DP problems you only need a subset of the full array
      (See Fibonacci DP...you only need the last 2 computed values)

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

    How would you change this solution if we can't rob from N adjacent houses? Do i need a more rob variables?

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

      I think so. This is the version optimized for space. Otherwise an array for the total amount of sub problems to solve for is used (number of all houses in this case). In the loop we would use something like arr[n] and arr[n-1] instead of rob1 and rob2 but we use them since we are only checking elements at these two indices at any given time.

  • @voltorian-minecraft
    @voltorian-minecraft ปีที่แล้ว

    i did some of the previous DP questions on the blind 75 with your videos and figured this out using bottom up approach, but my solution ended up being O(n^2) time, while this solution is just O(n). ugh...how can you tell which to use? also looking in the comments i see a O(n) bottom up solution...

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

    I find your approach helpful but there is one important thing missing: prove correctness. How do you guarantee that the algorithm computes the correct answer?

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

    Thanks for the videos, neat expiation👍
    What’s happens (3,2,2,3)

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

    I still have a hard time understanding how the code addresses the adjacency constrain. Nothing in the code looks at the indexes, or tracks whether or not we have already robbed the index before the current one.

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

    class Solution:
    def rob(self, nums: List[int]) -> int:
    rob1, rob2 = 0, 0
    for n in nums:
    rob1, rob2 = rob2, max(n + rob1, rob2)

    return rob2
    You don't need the temp variable, you can just swap on the same line.

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

    Easy Problem ??? Excellent Explanation

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

    if I want to do this problem by recursion and memoisation, what would be the code

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

    What if you have a testcase [2, 1, 3, 4]? By above solution, you will parse 2+3, 1+4 from which we get 5? But shouldn't the answer be 6 ie, (2+4)?

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

    i love your diagram

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

    Thanks !

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

      Thank you so much 🙏

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

    Hey, thanks for explaining these problems, they're incredibly helpful. Just one question: at 5:01 how come it is Rob[1:] and not Rob[1] + Rob[3:] like the previous Max skip?

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

      Thats same thing im trying to figure out

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

      @@marcusplenty1153 he’s just writing the relation between n to the rest of the array. If you rob the current house (i) then you can’t rob the next (i+1), but you can rob i+2.
      From i:n, It’s possible youll solve some of the same sub problems as 2:n. Thus the DP solution to cache your result.
      Again rob index to n in this recurrence relation will break down all the way to the last house and pack on the way back finding the max between picking the current house or not)

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

    nice!

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

    I just watched the beginning of the explanation and then went on with trying to understanding on my own. At the end my code used the bottom-down approach you seem to refer to in the explanatory part: recursive calls and memoization with Python dictionaries (without it, LeetCode gave me time limit exceeded). But I want to understand your code, which seems a bit unintuitive to me.

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

    Thanks

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

    what was the other version of leetcode where you used to solve problems which has like the premium version questions for leetcode for free

  • @MOHITRANA-to7rf
    @MOHITRANA-to7rf 2 ปีที่แล้ว

    wow nice video

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

    does your logic works if we have values
    15,7,9,100,1?

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

    I just did not understand that when you initialised rob1, rob2 to 0 then in the example which you gave how will rob1 and rob2 be at index 0 and 1, they both should be at index 0 only right? and also after you wrote n in nums then n will also start from 0, but you wrote that n will start after rob1 and rob2. please explain i am really confused. Thanks in advance

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

    How do you come up with this solutions ))) This is amazing

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

    8:40 ❤️❤️

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

    They bumped this problem up to Medium

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

    the naive way of thinking about this problem: dp[i] = max(arr[i] + dp[i - 2], dp[i - 1]), dp[i] stands for at position i, the max profit we can get.
    neet just uses the bottom-up solution plus the var1 and var2 for recording its status, since at each position, we don't need the whole arr, just its two previous positions.

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

    I am still so confused about this solution of code from rob1 = rob2. Am I stupid even I have watched this video for many times. Hmmmmm. And the important thing is, how can we know rob1 is before rob2. They are all equal to 0. and neetcode said this [rob1, rob2, n, n + 1,...], but before he wrote this list, he said rob1 and rob2 should be at last. how can these two change to the first one and second one? I am so confused. OMG. Gonna die.

  • @DJ-bo4pz
    @DJ-bo4pz ปีที่แล้ว +1

    This is one of the most underrated problems. Most people would be able to solve it, but would have a very superficial understanding of the why the solution works. This video illustrates very carefully as to WHY certain decisions were made while coming up with the solution eg. why don't you need a full dp array, why 2 variables suffice, why do you have to do rob1+n....it's an easy problem but the thinking that goes in is amazing. You really need to have a clear mind to explain it. Solving it is easy, explaining the solution is difficult.

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

    I was able to device a recursive brute force solution, but I could never come up with DP. How yall figured this solution out?

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

    Just a stupid question, but why we cannot just run two loops, one from the 0th index and one from the 1st index, skipping the adjacent elements and calculate the sum in two variables and return the max of the two ?

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

      Attempted this myself on LC and realized it's because there will be certain cases in which that answer will not be optimal/correct. For example, that approach will be correct if the input array was [1, 2, 3, 1] (return 4, which is the first and third elements together, which is larger than 3 which is the second + fourth elements). But if the input array is [2, 1, 2, 4], that approach will return 5 (second+ fourth elements, larger than first+third = 4) when the answer should be 6 (first+ fourth elements). In other words there will be input cases in which its is more beneficial to not rob the immediate next non-adjacent house.

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

    i come with this solution but did wrong for n = 2 :( now i got it :)

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

    Great

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

    Got a similar problem, but the robber cannot rob up to K consecutive houses. Do you have any solution? nice explaination btw

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

      @duong Tran: I guess it shud be similar with the above stated problem of 2 consecutive. No change in recurrence

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

      @@abhishekpawar8458 figured it out, pretty messy but it does the job xD

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

      @@duongtran642 glad to hear !

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

    Hi, great video! A thought I had after the solution: If I want also to return wish houses to be robbed, you know how I could do that?