Leetcode[198] House Robber
16 Nov 2015###Task1 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.
###Python
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums or len(nums) == 0:
return 0
N = len(nums)
dp = [0 for i in range(N + 1)]
dp[0] = 0
dp[1] = nums[0]
for i in range(2, N + 1):
dp[i] = max(dp[i - 2] + nums[i - 1], dp[i - 1])
return dp[N]
###Java
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}
int[] money = new int[nums.length + 1];
money[0] = 0;
money[1] = nums[0];
for (int i = 2; i <= nums.length; i++) {
money[i] = Math.max(money[i - 1], money[i - 2] + nums[i - 1]);
}
return money[nums.length];
}
}
###Points
动态规划(Dynamic Programming)
状态转移方程:
dp[i] = max(dp[i - 1], dp[i - 2] + num[i - 1])
其中,dp[i]表示打劫到第i间房屋时累计取得的金钱最大值。
时间复杂度O(n),空间复杂度O(n)
###Task2 Note: This is an extension of House Robber.
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.
###Python
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
if len(nums) == 1:
return nums[0]
left = nums[1:]
N = len(left)
dp = [0 for i in range(N + 1)]
dp[0] = 0
dp[1] = left[0]
for i in range(2, N + 1):
dp[i] = max(dp[i - 1], dp[i - 2] + left[i - 1])
right = nums[:-1]
dpp = [0 for i in range(N + 1)]
dpp[0] = 0
dpp[1] = right[0]
for i in range(2, N + 1):
dpp[i] = max(dpp[i - 1], dpp[i - 2] + right[i - 1])
return dp[N] if dp[N] > dpp[N] else dpp[N]
###Java
public class 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 nums[0] > nums[1] ? nums[0] : nums[1];
}
int[] moneyFirst = new int[nums.length];
moneyFirst[0] = 0;
moneyFirst[1] = nums[0];
for (int i = 2; i < nums.length; i++) {
moneyFirst[i] = Math.max(moneyFirst[i - 1], moneyFirst[i - 2] + nums[i - 1]);
}
int[] moneyLast = new int[nums.length];
moneyLast[0] = 0;
moneyLast[1] = nums[1];
for (int i = 2; i < nums.length; i++) {
moneyLast[i] = Math.max(moneyLast[i - 1], moneyLast[i - 2] + nums[i]);
}
return moneyFirst[nums.length - 1] > moneyLast[nums.length - 1] ? moneyFirst[nums.length - 1] : moneyLast[nums.length - 1];
}
}