DEV Community

Hector Williams
Hector Williams

Posted on

LeetCode #198. House Robber

Problem

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Example 1:

Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:

Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.

Constraints:

1 <= nums.length <= 100
0 <= nums[i] <= 400

Discussion

The problem can be solved using dynamic programming.We create a new array dp and use it to store the maximum up to a certain index. The first index has the value stored at the first index in nums.The second one takes the maximum of the first 2 values in nums.
We then iterate through the rest of the array and store in dp,the maximum of the previous value in dp and the sum of the current value in nums and the value before the previous value in dp. We return the last value in dp once we are done traversing.

Java

class Solution {
    public int rob(int[] nums) {
         if(nums.length==0) return 0;
         if(nums.length==1)return nums[0];
         if(nums.length==2)return Math.max(nums[0],nums[1]);
         int [] loot = new int[nums.length];
          loot[0]= nums[0];
          loot[1]= Math.max(nums[1],nums[0]);
          for(int i=2;i<loot.length;i++){
            loot[i]= Math.max(nums[i]+loot[i-2],loot[i-1]);
          }
          return loot[loot.length-1];
    }
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

var rob = function(nums) {
  if(nums.length==0) return 0;
  if(nums.length==1)return nums[0];
  if(nums.length==2)return Math.max(nums[0],nums[1]);
  const loot = new Array(nums.length);
  loot[0]= nums[0];
  loot[1]= Math.max(nums[1],nums[0]);
  for(let i=2;i<loot.length;i++){
   let previousloot= loot[i-1];
   let currentloot= nums[i]+loot[i-2];
   loot[i]= Math.max(currentloot,previousloot);
  }
  return loot[loot.length-1];
};
Enter fullscreen mode Exit fullscreen mode

C#

public class Solution {
    public int Rob(int[] nums) {
     if(nums.Length==0) return 0;
     if(nums.Length==1)return nums[0];
     if(nums.Length==2)return Math.Max(nums[0],nums[1]);
     int [] loot = new int[nums.Length];
     loot[0]= nums[0];
     loot[1]= Math.Max(nums[1],nums[0]);
     for(int i=2;i<loot.Length;i++)loot[i]= Math.Max(nums[i]+loot[i-2],loot[i-1]);
     return loot[loot.Length-1];   
    }
}

Enter fullscreen mode Exit fullscreen mode

C++

class Solution {
public:
    int rob(vector<int>& nums) {
     if(nums.size()==0) return 0;
     if(nums.size()==1)return nums.at(0);
     if(nums.size()==2)return std::max(nums.at(0),nums.at(1));
     vector<int> loot;
     loot.push_back(nums.at(0));
     loot.push_back(std::max(nums.at(1),nums.at(0)));
     for(int i=2;i<nums.size();i++){
      loot.push_back(std::max(nums.at(i)+loot.at(i-2),loot.at(i-1)));
     }
     return loot.at(loot.size()-1);  
    }
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)