题目来源【Leetcode】
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) {
int f1 = 0;
int f2 = 0;
for (int i=0; i<nums.size(); i++){
if (i%2==0) f1 = max(f1+nums[i], f2);
else f2 = max(f1, f2+nums[i]);
}
return max(f1, f2);
}
};

探讨了如何通过动态规划解决专业窃贼抢劫问题,在不触动报警系统的前提下,求解能从一排房屋中偷窃到的最大金额。文章提供了一个具体的C++实现方案。
232

被折叠的 条评论
为什么被折叠?



