LeetCode 198. House Robber (Algorithm Explained)

Sdílet
Vložit
  • čas přidán 20. 12. 2019
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
    AlgoCademy - algocademy.com/?referral=nick...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
    Follow My Twitter - / nicholaswwhite
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nickwwhite?locale.x...
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering
  • Věda a technologie

Komentáře • 69

  • @djBC10000
    @djBC10000 Před 4 lety +122

    yo you should have kept the robber mask while doing the question

  • @guitarvoicing
    @guitarvoicing Před rokem +10

    This in old and I don't know if you ever read your subscribers comments, but I have to tell you one amazing thing about you. You have an incredible talent to explain complicated things in a very simple way. Yet, there is an extra impressive feature on your methodology of explaining things. You explain them really fast. Normally, one expect to make it more difficult to follow. But it is the other way around with your videos. You explains things very fast *AND* make them easy to understand. I don't think you even realize this. It is a bit antagonist, how can you explain it so well and so fast? But, man it works great. Thanks for posting these solutions with fast and easy explanations.

  • @ryan.aquino
    @ryan.aquino Před 4 lety +23

    Explaining the template for dynamic programming did helped me.

  • @quangvo4563
    @quangvo4563 Před 4 lety +25

    Hi Nick, can you create a path or a map for beginners to follow which algorithm /data structure should they learn first and what problems should they do first.

  • @Edysamaha
    @Edysamaha Před 2 lety

    Finally i understood this problem! Thank you... Also the fact that you used an array instead of a variable to show the progression of the maxvalue helped to understand

  • @oscaropdyou
    @oscaropdyou Před 4 lety +4

    Thanks for the video. Here is slightly modified/improvised code:
    int dp[] = new int[nums.length];
    dp[0] = nums[0]; dp[1] = Math.max(dp[0], nums[1]);
    for(int i=2; i

    • @aba0101
      @aba0101 Před 2 lety

      However, this solution will cause ArrayIndexOutofBounds unless you put another if(nums.length == 1) return dp[1].

  • @LoXatoR94
    @LoXatoR94 Před 3 lety +2

    I think apart from Fibonacci, this is the first time I truly understood a DP problem, even though its considered Easy. Thank you Nick, You explain really really well!

  • @kittwwang
    @kittwwang Před 3 lety +4

    The template is really helpful! Very clear explanation again!

  • @emmanuelonwumah915
    @emmanuelonwumah915 Před 4 lety +24

    You lost me from 8:47 to 9:52 and sadly that the most important part :(

  • @chelseakatsidzira7153
    @chelseakatsidzira7153 Před 4 lety +3

    you really have a gift for this. i've been struggling to understand this when all i had to do was watch a couple of your videos to get it. THANK YOUUUUU

  • @PR1V1LE6ED
    @PR1V1LE6ED Před rokem

    Great explanation. I like how you made clear what the template was for a dp problem.

  • @firefly2008god
    @firefly2008god Před 4 lety +7

    Hello Nick. You explained this very nicely. Could you please make a playlist exclusively for Dynamic Programming problems? That would be extremely helpful. Thanks.

  • @dera_ng
    @dera_ng Před rokem +2

    I find it easier to solve the problems after understanding a task via a decision tree. However after building out the decision tree and seeing that max_money_stealable is dependent on amount of money the robber has stolen so far, it became pretty difficult to move forward with.
    For those who have MASTERED DP, this is probably an easy problem. For those that are still looking for easily digestible/understandable material on this topic called "Dynamic Programming", it's not easy.

  • @nevikgnehz368
    @nevikgnehz368 Před 2 lety

    11:00 There should be 4 elements. On line 5 you declared nums.length + 1 so [1,2] should be of array size of 3 and since array is zero based it goes from 0th index to 3rd which is total of 4 spaces.

  • @rupaldesai7098
    @rupaldesai7098 Před 4 lety +1

    You are a good sarcastic comedian

  • @ahmedz7414
    @ahmedz7414 Před 4 lety +4

    Nick I dont usually comment but this was a really good video. I’ve seen videos before about dynamic programming but they all made it seemed like this really advanced topic for no reason. When its just storing values in an array. Please make more videos just explaining each category of problems in simple terms this video was really helpful. 👍

  • @iamkhanhnguyen
    @iamkhanhnguyen Před 3 lety

    Great explanation. You save my day bro

  • @dalgonacovfefe
    @dalgonacovfefe Před 4 lety +1

    Did you ever end up doing that video on the main types of problems and how to spot them?

  • @anuragv400
    @anuragv400 Před 3 lety

    Thnx man ! It helped a lot :)

  • @andreandrews6237
    @andreandrews6237 Před 4 lety +4

    Could have used Kadane's Algorithm to do this in O(1) space :) with the same runtime
    if (nums.Length == 0) return 0;
    if (nums.Length < 2 ) return nums[0];
    int ans = Math.Max(nums[0], nums[1]);
    int a = nums[0];
    for (int i = 2; i < nums.Length; i++)
    {
    var temp = Math.Max(ans, a + nums[i]);
    a = ans;
    ans = temp;
    }
    return ans;
    ^^ for reference if anyone was wondering (C#) implementation

  • @ramkrishnasingh56
    @ramkrishnasingh56 Před 4 lety

    Hey Nick I would like to solve all the problem you taught , is there any path in which I should do the questions ?

  • @nknidhi321
    @nknidhi321 Před 3 lety

    Thanks Nick!!

  • @sophia0282
    @sophia0282 Před 2 lety

    Can you please explain why the templet always set as int dp[] = new int [nums.length + 1]? What does mean to add 1? I still don't understand.

  • @prepat2133
    @prepat2133 Před rokem

    dude i'm literally only applying for SWE internship as a college junior and most of my OAs had dynamic programming. I'm not even joking.

  • @ujjvalsharma5055
    @ujjvalsharma5055 Před 4 lety

    Hey nick if you did a video on dynamic programming where you are explains types of dynamic programming question. Please send the link. I would like to watch.

  • @Rahulyadav-lv7dh
    @Rahulyadav-lv7dh Před 2 lety

    immaculate explanation

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

    Great explanation

  • @curesnow6493
    @curesnow6493 Před 2 lety

    Hello Nick,
    I wish I am very good at solving coding problems without looking at the solutions. What are your advice?

  • @rogerchou7762
    @rogerchou7762 Před 3 lety

    Dude you are awesome!

  • @ahmadtibi7744
    @ahmadtibi7744 Před 4 lety +1

    what's up with the camera quality nick.. when you used your macbook the camera quality was better

  • @prakad97
    @prakad97 Před 4 lety +1

    Great understandable explanation, As we need only last two values, we can just maintain two variables and get the answer..that way space complexity would be O(1)..
    Edit.: For those who missed it in video.!

    • @NickWhite
      @NickWhite  Před 4 lety +6

      mentioned in the video

    • @prakad97
      @prakad97 Před 4 lety +1

      @@NickWhite guess i skipped over that part..😅

  • @MIDNightPT4
    @MIDNightPT4 Před 2 lety

    "Not your morals" lmaoooooooo

  • @Jeremy-dh6ks
    @Jeremy-dh6ks Před 4 lety

    Happy Holidays Nick. I saw in your previous videos that you used a mac, and now you are using windows. Any particular reason why you switched?

    • @NickWhite
      @NickWhite  Před 4 lety +3

      I bought a gaming PC and it’s pretty good for live streaming so I’m just messing around with it atm Happy holidays to you as well!

  • @SudhanshuSrivastavaIndia

    Hey Nick,
    Just curious why we need an array "dp" when we already know that last element is going to be the result. Can we simply have 2 variables one to store prev-result and another for fina-result and do the same thing. Help me if I am missing anything...
    func rob(_ nums: [Int]) -> Int {
    guard nums.count > 0 else { return 0 }
    var prevResult: Int = 0
    var result: Int = nums[0]
    for i in 1..

  • @LiLzViEtzThuGGn
    @LiLzViEtzThuGGn Před 3 lety

    You cracked me up lol.

  • @rathan235
    @rathan235 Před rokem

    Great job

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 Před 4 lety

    Good one, can you make video for dp

  • @yuvrajdarekar3494
    @yuvrajdarekar3494 Před 3 lety +2

    That thumbnail though😈😈

  • @Stephen_luk
    @Stephen_luk Před 2 lety

    It helps , thanks.

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

    love you man

  • @thecarrotdude
    @thecarrotdude Před rokem

    Think again buster!

  • @adithyaas2324
    @adithyaas2324 Před 2 lety

    man you are very fast!

  • @tanmaydeshpande2458
    @tanmaydeshpande2458 Před 3 lety

    Think again buster! LMAO!

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

    I still didn't see how the code covered that edge case of jumping two houses and grab the third, even though your code passed the edge use cases...
    EDIT: Did a table test an got it:
    dp [0, 40, 40, 44, 50]
    40, 2, 4, 10

  • @aumrudhlalkumartj1948
    @aumrudhlalkumartj1948 Před 2 lety

    Thanks

  • @AdityaSahu95
    @AdityaSahu95 Před 3 lety

    Upvoting just because of that thumbnail xD

  • @vikramreddy7586
    @vikramreddy7586 Před 4 lety

    The rap king!

  • @ManishKumar-rz9ub
    @ManishKumar-rz9ub Před 3 lety +1

    This is not memoization. This is tabulation, a bottom up approach.

    • @sujaysreedhar8313
      @sujaysreedhar8313 Před 3 lety

      Yes, thanks for saying that... I pretty much broke my head thinking of how it would be a memoization

  • @ashishkarn9283
    @ashishkarn9283 Před 2 lety

    How about this,
    public int rob(int[] nums) {
    if(nums.length>1){
    nums[1]=Math.max(nums[0],nums[1]);
    }
    for (int i = 2; i < nums.length; i++) {
    nums[i]=Math.max(nums[i]+nums[i-2],nums[i-1]);
    }
    return nums[nums.length-1];
    }

  • @Jeremy-dh6ks
    @Jeremy-dh6ks Před 4 lety

    0:45 WideHard

  • @shohanur_rifat
    @shohanur_rifat Před 4 lety +1

    It can be one of two options. 1+3+..... +till the end or 2+4 +....+till the end since they can't be negative

    • @mgancarzjr
      @mgancarzjr Před 3 lety +1

      Not necessarily. If two very large numbers are 5 indexes away from one another, they can be combined e.g.:
      1, 100, 1, 1, 1, 1, 100, 1
      Your method would return a max of 103 where the maximum is actually 201.

  • @nischayrawat682
    @nischayrawat682 Před 2 lety

    every time he says its the easiest problem i m like that isnt true that's why i am watching your video

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

    Well now it is medium :)

  • @akanshkumar2876
    @akanshkumar2876 Před 3 lety

    u deserve a like for that mask

  • @Cruzylife
    @Cruzylife Před rokem

    wheres the study guide

  • @YNA64
    @YNA64 Před 4 lety

    "Hello guys it's Nick White chushdskfjdksjfksjfks INFORMATION ksjdfkdj House Robber ". hHhahaha jk

  • @LoneWolf-th6gn
    @LoneWolf-th6gn Před rokem

    Boom!😆

  • @QuickstickD
    @QuickstickD Před 2 lety

    You lost me from 00:00 - 13:25 😁 I’m joking I’m just watching because I want to learn.

  • @jonathontucker
    @jonathontucker Před 4 lety

    This is helpful even if you aren't using java

  • @amansinhparmar3336
    @amansinhparmar3336 Před 4 lety

    Boom

  • @ahmedfattah5879
    @ahmedfattah5879 Před 4 lety

    17

  • @fettuccine794
    @fettuccine794 Před rokem

    This is now under Medium category

  • @tkokflux6322
    @tkokflux6322 Před rokem

    bro is in 8k