算法分析与设计——LeetCode Problem.198 House Robber
2017-12-29 12:01
281 查看
题目链接
问题描述
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.
解题思路
较为简单的动态规划,递推式为dp[i] = max(dp[i-1], dp[i-2] + num[i])
代码如下
class Solution {
public:
int rob(vector<int>& nums) {
int leng = nums.size();
if (leng == 0) return 0;
if (leng == 1) return nums[0];
if (leng == 2) return max(nums[0], nums[1]);
int sum = 0;
vector<int> maxVec(leng, 0);
maxVec[0] = nums[0];
maxVec[1] = max(nums[0], nums[1]);
for (int i = 2; i < leng; i++) {
maxVec[i] = max(maxVec[i - 1], maxVec[i - 2] + nums[i]);
}
return maxVec[leng - 1];
}
};
问题描述
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.
解题思路
较为简单的动态规划,递推式为dp[i] = max(dp[i-1], dp[i-2] + num[i])
代码如下
class Solution {
public:
int rob(vector<int>& nums) {
int leng = nums.size();
if (leng == 0) return 0;
if (leng == 1) return nums[0];
if (leng == 2) return max(nums[0], nums[1]);
int sum = 0;
vector<int> maxVec(leng, 0);
maxVec[0] = nums[0];
maxVec[1] = max(nums[0], nums[1]);
for (int i = 2; i < leng; i++) {
maxVec[i] = max(maxVec[i - 1], maxVec[i - 2] + nums[i]);
}
return maxVec[leng - 1];
}
};
相关文章推荐
- [算法分析与设计] leetcode 每周一题: 201. Bitwise AND of Numbers Range
- 算法分析与设计课程(15):【leetcode】 Find Minimum in Rotated Sorted Array
- [算法分析与设计] leetcode 每周一题: 070. Climbing Stairs
- 算法分析与设计——LeetCode Problem.3 Longest Substring Without Repeating Characters
- 算法分析与设计——LeetCode:3. Longest Substring Without Repeating Characters
- 算法分析与设计课程(6):【leetcode】Sudoku
- [算法分析与设计] leetcode 每周一题: 542. 01 Matrix
- 算法分析与设计课程(18):【leetcode】Clone Graph
- 算法分析与设计——LeetCode:5. Longest Palindromic Substring
- 算法分析与设计——LeetCode:39. Combination Sum
- 算法分析与设计——leetcode刷题之Add Two Numbers(Medium)
- [算法分析与设计] leetcode 每周一题: Minimum Time Difference
- 算法分析与设计课程——LeetCode刷题之 Rotate Image
- 算法分析与设计——LeetCode Problem.63 Unique Paths II
- 算法分析与设计——LeetCode:101. Symmetric Tree
- 算法分析与设计丨第六周丨LeetCode(9)——Course Schedule(Medium)
- 算法分析与设计课程(14):【leetcode】 Maximal Square
- 算法分析与设计丨第二周丨LeetCode(4)——Maximum Subarray(Easy)
- 算法分析与设计课程——LeetCode刷题之Search for a Range
- [算法分析与设计] leetcode 每周一题: Surrounded Regions