198 House Robber
来源:互联网 发布:淘宝密码怎么修改 编辑:程序博客网 时间:2024/04/30 04:28
Leetcode198:
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.
这是一道典型的线性规划问题,假设有n个房子,编号0~n-1,令抢劫第i个房子时已经抢到的钱最多,则:
抢劫第i个房子 f(i) = num[i] + g(i+1)
不抢劫第i个房子 g(i) = max( f(i) , g(i) )
class Solution {public:int rob(vector<int> &num) {int f = 0, g = 0, pref = 0, preg = 0, s = 0;for (int i = num.size() - 1; i >= 0;i--){f = preg + num[i];g = max(pref, preg);pref = f;preg = g;}return max(f, g);}};
0 0
- Leetcode 198: House Robber
- 198House Robber
- leetcode-198 House Robber
- [LeetCode 198]House Robber
- 198 House Robber
- House Robber - LeetCode 198
- leetcode 198 House Robber
- 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
- ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
- 机器学习入门的书单(数据挖掘、模式识别等一样)
- 马赫带现象
- zoj 3862 Intersection(枚举)
- 超实用的JavaScript技巧及最佳实践(下)
- 198 House Robber
- http知识点
- 如何在eclipse cdt环境下 导入一个含makefile的工程文件
- Mybatis的几点小结
- iOS中block介绍(三)揭开神秘面纱(上)
- 数据结构关于AOV与AOE网的区别
- 感受亮度与光强,及马赫带效应
- 摘-学习网络编程方法
- github之别名