leetcode198
来源:互联网 发布:java开发业务流程 编辑:程序博客网 时间:2024/06/04 23:32
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.
题意:
给出一个nums的vector,从中选取一些数字,不能连续选择两个,最后使得所选取的数字之和最大。
解法:
DP,状态转移 方程:mmax[i]=max(mmax[i-2]+num[i], mmax[i-1])。
代码:
class Solution {public: int rob(vector<int>& nums) { int len=nums.size(); if(len==0) return 0; if(len==1) return nums[0]; vector<int> mmax; mmax.push_back(nums[0]); mmax.push_back(max(nums[0], nums[1])); for(int i=2; i<len; i++){ mmax.push_back(max(nums[i]+mmax[i-2], mmax[i-1])); } return mmax[len-1]; }};
0 0
- leetcode198
- leetCode198-House Robber
- leetcode198. House Robber
- Leetcode198: Minimum Height Trees
- leetcode198-House Robber
- LeetCode198. House Robber
- leetcode198.[DP]House Robber
- leetcode198-House Robber
- leetcode198 House Robber
- LeetCode198—House Robber
- LeetCode198 House Robber
- LeetCode198题:House Robber【java】
- LeetCode198——House Robber
- Leetcode198——House Robber
- leetcode198和213 House Robber I and II---java实现
- [LeetCode198 House Robber]数组中的动态规划求最值问题
- Leetcode198.+算法期中1005. 动态规划问题之最大最小和问题
- woowj
- 算法作业_10(2017.3.23第五周)
- Spark商业案例与性能调优实战100课》第36课:彻底解密Spark 2.1.X中Sort Shuffle中Reducer端源码内幕.pptx>>
- 看zookeeper的事物日志
- A Knight's Journey
- leetcode198
- maven 依赖jar包的查询
- javacript中的this到底是什么
- Pandas 求余运算
- 【Anaconda】conda环境管理和包管理
- ESP8266--学习笔记(八)串口源码分析
- 从一个游戏开始看设计(三)
- golang 实现HTTP代理和反向代理
- 二叉查找树