LeetCode 198. House Robber
2018-01-19 00:27
387 查看
题目描述
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.
具体实现
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() == 0) return 0;
vector< vector<int> > sum(nums.size(), vector<int>(2, 0));
sum[0][1] = nums[0];
int max = nums[0];
for (int i = 1; i < nums.size(); i++) {
sum[i][0] = sum[i-1][1] > sum[i-1][0] ? sum[i-1][1] : sum[i-1][0];
sum[i][1] = sum[i-1][0] + nums[i];
max = max > sum[i][0] ? max : sum[i][0];
max = max > sum[i][1] ? max : sum[i][1];
}
return max;
}
};
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.
具体实现
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() == 0) return 0;
vector< vector<int> > sum(nums.size(), vector<int>(2, 0));
sum[0][1] = nums[0];
int max = nums[0];
for (int i = 1; i < nums.size(); i++) {
sum[i][0] = sum[i-1][1] > sum[i-1][0] ? sum[i-1][1] : sum[i-1][0];
sum[i][1] = sum[i-1][0] + nums[i];
max = max > sum[i][0] ? max : sum[i][0];
max = max > sum[i][1] ? max : sum[i][1];
}
return max;
}
};
相关文章推荐
- LeetCode 198. House Robber
- [LeetCode] 198. House Robber 打家劫舍
- Leetcode:198. House Robber(week 9)
- LeetCode-198. House Robber
- LeetCode 198. House Robber
- leetcode_198. House Robber
- 198. House Robber,又发现一道数学归纳法在leetcode中的应用题
- leetcode 198. House Robber
- Leetcode 198. House Robber
- leetcode 198. House Robber
- [Leetcode] 198. House Robber 解题报告
- leetcode 198. House Robber
- LeetCode-198. House Robber (Java)
- 算法17: leetcode 198. House Robber
- LeetCode 198. House Robber
- LeetCode 198. House Robber
- LeetCode 198. House Robber
- 【LeetCode】198. House Robber
- [LeetCode]problem 198. House Robber
- Leetcode 198. House Robber (Easy) (cpp)