[LeetCode] House Robber
2015-08-15 11:12
411 查看
Since we are not allowed to rob two adjacent houses, we keep two variables
The code is as follows.
preand
cur. During the
i-th loop,
prerecords the maximum profit that we do not rob the
i - 1-th house and thus the current house (the
i-th house) can be robbed while
currecords the profit that we have robbed the
i - 1-th house.
The code is as follows.
class Solution { public: int rob(vector<int>& nums) { int n = nums.size(), pre = 0, cur = 0; for (int i = 0; i < n; i++) { int temp = max(pre + nums[i], cur); pre = cur; cur = temp; } return cur; } };
相关文章推荐
- 【编程】float详解
- 华为OJ平台试题 —— 字符串:字符串最后一个单词的长度
- css
- 完整数独局面的生成算法
- python字符串处理二 内建函数
- C++ 内存分布
- 创建动态数组以及sort函数的用法 -- 字符串的漂亮度
- IE6 7 下右浮动元素自动换行问题
- STM32 下的库函数和寄存器操作比较
- STM32 下的库函数和寄存器操作比较
- hdu 4604(LIS强化版)
- 我对Markdown的体验和感受
- STM32 下的库函数和寄存器操作比较
- STM32 下的库函数和寄存器操作比较
- 实现Pop3s&maildrop(有错待解决)
- 通过格式良好的SQL提高效率和准确性
- 常用Sql语句及sql数据类型
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
- 小贝_php+redis简单实例
- 【基础练习】【线性DP+离散化】codevs1105 过河题解