House Robber
题目地址:
https://leetcode.com/problems/house-robber/description/
题目:
解题思路:
we could solve this problem using dynamic programming.
代码:
Here is another solution:
Follow up:
https://leetcode.com/problems/house-robber/description/
题目:
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 system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
解题思路:
we could solve this problem using dynamic programming.
代码:
public int rob(int[] nums) { if(nums == null || nums.length == 0){ return 0; } // the first col is the result with robbing the curr house // the second col the result on not robbing the curr house. int[][] dp = new int[nums.length][2]; for(int i = 0; i <= nums.length - 1; i++){ if(i == 0){ dp[i][0] = 0; dp[i][1] = nums[i]; continue; } dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]); dp[i][1] = nums[i] + dp[i - 1][0]; } return Math.max(dp[nums.length - 1][0], dp[nums.length - 1][1]); }
Here is another solution:
public int rob(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
if(nums.length == 1){
return nums[0];
}
if(nums.length == 2){
return Math.max(nums[0], nums[1]);
}
int[] dp = new int[nums.length + 1];
dp[0] = 0;
dp[1] = nums[0];
for(int i = 2; i <= nums.length; i++){
dp[i] = Math.max(nums[i - 1] + dp[i - 2], dp[i - 1]);
}
return dp[nums.length];
}
Follow up:
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
public class HouseRobberII { public static void main(String[] args){ HouseRobberII houseRobber = new HouseRobberII(); int[] nums = {1, 1, 1}; int rst = houseRobber.rob(nums); System.out.println(rst); } public int rob(int[] nums) { if(nums == null || nums.length == 0){ return 0; } if(nums.length == 1){ return nums[0]; } // the first col is the result with robbing the curr house // the second col the result on not robbing the curr house. int rst1 = helper(nums, true); int rst2 = helper(nums, false); return Math.max(rst1, rst2); } private int helper(int[] nums, boolean robFirst){ int[][] dp = new int[nums.length][2]; for(int i = 1; i <= nums.length - 1; i++){ if(i == 1){ if(robFirst){ dp[i][0] = nums[i - 1]; dp[i][1] = nums[i - 1]; } else{ dp[i][0] = dp[i - 1][0]; dp[i][1] = nums[i]; } continue; } if(i == nums.length - 1){ if(robFirst){ return Math.max(dp[i - 1][0], dp[i - 1][1]); } } dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]); dp[i][1] = nums[i] + dp[i - 1][0]; } return Math.max(dp[nums.length - 1][0], dp[nums.length - 1][1]); } }
Another solution:
public int rob(int[] nums) { if (nums.length == 0) return 0; if (nums.length < 2) return nums[0]; int[] startFromFirstHouse = new int[nums.length + 1]; int[] startFromSecondHouse = new int[nums.length + 1]; startFromFirstHouse[0] = 0; startFromFirstHouse[1] = nums[0]; startFromSecondHouse[0] = 0; startFromSecondHouse[1] = 0; for (int i = 2; i <= nums.length; i++) { startFromFirstHouse[i] = Math.max(startFromFirstHouse[i - 1], startFromFirstHouse[i - 2] + nums[i-1]); startFromSecondHouse[i] = Math.max(startFromSecondHouse[i - 1], startFromSecondHouse[i - 2] + nums[i-1]); } return Math.max(startFromFirstHouse[nums.length - 1], startFromSecondHouse[nums.length]); }
Follow up:
代码:
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
3 / \ 2 3 \ \ 3 1Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.代码:
public int rob(TreeNode root){ if(root == null){ return 0; } int[] rst = helper(root); return Math.max(rst[0], rst[1]); } private int[] helper(TreeNode root){ if(root == null){ return new int[2]; } int[] rst = new int[2]; int[] left = helper(root.left); int[] right = helper(root.right); rst[0] = root.val + left[1] + right[1]; rst[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); return rst; }

Comments
Post a Comment