House Robber -- leetcode
来源:互联网 发布:如何重置手机网络设置 编辑:程序博客网 时间:2024/05/21 09:49
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.
基本思路:动态规划
设cash[i] 是抢劫第i所(含)以前所有房子得到的现金。
则cash[i+1] = max( nums[i+1] + cash[i-1], cash[i])
即,如果再加入一所房子,所能是到最大现金的方式为两种:
1, 是抢劫当前房子,并加上,抢动前面房子所得到的最大现金(不包含前面紧临的房子)
2, 不抢劫当前房子,实际得到的现金,为抢动前面房子所得的最大现金(包含前面坚临的房子)
取两者途径较大者,为抢劫当前房子(含)之前所得到的最多大现金。
实际运算中,不需要一维数组。只需要两个变量就成。分别保存上一次,和上上一次计算结果。
class Solution {public: int rob(vector<int>& nums) { int last = 0; int last_last = 0; for (auto i: nums) { int current = max(i+last_last, last); last_last = last; last = current; } return last; }};
- 【dp】Leetcode House Robber&& House Robber II
- 【leetcode】House Robber && House Robber II
- LeetCode --- House Robber & House Robber II
- LeetCode OJ House Robber
- Leetcode: House Robber
- Leetcode 198: House Robber
- House Robber - LeetCode
- LeetCode 之 House Robber
- leetcode--House Robber
- [leetcode]47 House Robber
- [LeetCode] House Robber
- LeetCode :House Robber
- [leetcode] House Robber
- [leetcode]House Robber
- [LeetCode]198.House Robber
- LeetCode House Robber题解
- leetcode:House Robber
- [LeetCode]House Robber
- 【C语言】二维数组中的查找,杨氏矩阵
- 【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都按照从左到右的递增的顺序排序,输入这样的一个数组和一个数,判断数组中是否包含这个数
- MySQL详解(15)-----------海量数据讲解
- 周记——20150706
- Mysql bit类型带来的坑
- House Robber -- leetcode
- 前端(移动端)开发利器Chrome Developer Tools秘籍(下)
- ibatis和mybatis的区别
- 计算空间中两条线段的最小距离
- iOS 网络编程 GCDasynsocket socket 发送数据
- 父、子容器高度不定,实现垂直居中
- 导出和导入
- 修改cas-server3.5.1(一) cas取消https方法配置。
- multimap一对多映射