198. House Robber 类别:动态规划 难度:easy
2017-01-08 22:15
351 查看
题目:
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.
思路:
转移方程res[i] = max(nums[i - 1] + res[i - 2],res[i - 1])。
程序:
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)
return 0;
vector<int> res(nums.size() + 1,0);
res[1] = nums[0];
for(int i = 2;i <= nums.size();i++)
res[i] = max(nums[i - 1] + res[i - 2],res[i - 1]);
return res[nums.size()];
}
};
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.
思路:
转移方程res[i] = max(nums[i - 1] + res[i - 2],res[i - 1])。
程序:
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)
return 0;
vector<int> res(nums.size() + 1,0);
res[1] = nums[0];
for(int i = 2;i <= nums.size();i++)
res[i] = max(nums[i - 1] + res[i - 2],res[i - 1]);
return res[nums.size()];
}
};
相关文章推荐
- 121. Best Time to Buy and Sell Stock 类别:动态规划 难度:easy
- 动态规划——198. House Robber[easy]
- 70. Climbing Stairs 类别:动态规划 难度:easy
- 309. Best Time to Buy and Sell Stock with Cooldown 类别:动态规划 难度:medium
- 300. Longest Increasing Subsequence 类别:动态规划 难度:medium
- 376. Wiggle Subsequence 类别:动态规划 难度:medium
- 120. Triangle 类别:动态规划 难度:medium
- 63. Unique Paths II 类别:动态规划 难度:medium
- 198. House Robber(动态规划--房屋强盗)
- 357. Count Numbers with Unique Digits 类别:动态规划 难度:medium
- 343. Integer Break 类别:动态规划 难度:medium
- 62. Unique Paths 类别:动态规划 难度:medium
- 413. Arithmetic Slices 类别:动态规划 难度:Medium
- 64. Minimum Path Sum 类别:动态规划 难度:medium
- 96. Unique Binary Search Trees 类别:动态规划 难度:medium
- Leetcode-198_ House Robber-典型动态规划-【C++】
- robber问题的动态规划解法
- 动态规划——70. Climbing Stairs[easy]
- 动态规划专题之----213. House Robber II
- leetcode 198House Robber(简单动态规划解法)