DEV Community

Flame Chan
Flame Chan

Posted on

LeetCode Day33 Dynamic Programming

198. House Robber

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
Original Page

    public int rob(int[] nums) {
        int[] dp = new int[nums.length+1];
        dp[1] = nums[0];

        for(int i=2; i<dp.length; i++){
            dp[i] = Math.max(dp[i-2]+nums[i-1], dp[i-1]);
        }
        // System.out.println(Arrays.toString(dp));
        return dp[dp.length-1];

    }
Enter fullscreen mode Exit fullscreen mode

213. House Robber II

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system 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 = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
Example 2:

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 3:

Input: nums = [1,2,3]
Output: 3

Constraints:

1 <= nums.length <= 100
0 <= nums[i] <= 1000
Original Page

Wrong Code

    public int rob(int[] nums) {
        int[] dp = new int[nums.length+1];

        boolean findFirst = false;

        dp[1] = nums[0];

        if(nums.length>2){
            if(nums[0]+nums[2] > nums[1]){
                dp[2] = Math.max(dp[1], nums[1]);
                dp[3] = nums[0] + nums[2];
                findFirst = true;
            }else{
                dp[2] = Math.max(dp[1], nums[1]);
                dp[3] = dp[2];
            }
        }

        for(int i=4; i<dp.length; i++){
            if(i == dp.length-1){
                if(findFirst){
                    dp[i] = dp[i-1];
                    break;
                }
            }
            dp[i] = Math.max(dp[i-1], dp[i-2]+nums[i-1]);
        }
        System.out.println(Arrays.toString(dp));
        return dp[dp.length-1];
Enter fullscreen mode Exit fullscreen mode

in this case, we cannot cover the whole possibility.

    public int rob(int[] nums) {

        return Math.max(robber(nums, 0,nums.length-1), robber(nums,1,nums.length));


    }
    //dp[0] = 0 dp[1] = nums[1], dp[2] = max
    // begin 0, begin 1
        // [0,size-1] or [1, size]z
    public int robber(int[] nums, int beg, int end){
        int[] dp = new int[nums.length];
        if(beg == end){
            return nums[0];
        }
        dp[1] = nums[beg];

        for(int i=2; i<dp.length; i++){
            dp[i] = Math.max(dp[i-1], dp[i-2]+nums[beg + i - 1]);
        }
        // System.out.println(Arrays.toString(dp));
        return dp[dp.length-1];
    } 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)