[leetcode 198]House Robber
来源:互联网 发布:python eval() 编辑:程序博客网 时间:2024/05/21 14:09
Question:
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.
Solution:
<span style="font-size:14px;">class Solution {public: int rob(vector<int>& nums) { /* 采用动态规划法 fk[i]表示第i个阶段(房子数量为i)时候的最大钱数,可以知道fk[0] = 0; fk[1]=nums[0]; fk[2] = max(nums[0],nums[1]) = max(fk[0]+nums[1],fk[1]); 多列几个阶段的策略即可看出规律!----fk[i] = max((fk[i-2]+nums[i-1]),fk[i-1]);其中nums[i-1]对应第i个房子里的钱。 */ const int size = nums.size(); vector<int> fk(size+1); if(nums.empty()) return 0; else{ if(nums.size() == 1) return nums[0]; else{ fk[0] = 0; fk[1] = nums[0]; for(int i = 2; i <= size; i++){ fk[i] = max((fk[i-2]+nums[i-1]),fk[i-1]); } return fk[size]; } } }};</span>
0 0
- Leetcode 198: House Robber
- leetcode-198 House Robber
- [LeetCode 198]House Robber
- House Robber - LeetCode 198
- leetcode 198 House Robber
- leetCode #198 House Robber
- LeetCode 198-House Robber
- Leetcode[198]-House Robber
- 【Leetcode】House Robber #198
- Leetcode 198 House Robber
- leetcode 198:House Robber
- House Robber (leetcode 198)
- 【LeetCode】198:House Robber
- leetcode[198]:House Robber
- leetcode-198-House Robber
- leetcode 198: House Robber
- [LeetCode 198]House Robber
- Leetcode# 198 House Robber
- 玩转树莓派--网卡配置
- 商业模式(二):P2P网贷平台,利差和服务费为主的金融玩法
- 阅读有关软件工程与计算机科学区别的文章,谈谈你的看法。
- 树---判断两棵树是否相等
- Codevs P1982 加密算法
- [leetcode 198]House Robber
- VM VirtualBox虚拟机共享文件夹(ubuntu)
- 语义分析的一些方法(下篇)
- 2015年10月份总结
- HDOJ 5551 Huatuo's Medicine (水)
- Chapter5-标准I/O库(补充)-「APUE读书笔记」
- java程序内存体现
- 【PAT】1053. Path of Equal Weight (30)
- 【读书笔记】程序员的自我修养总结(七)