leetcode笔记:House Robber
来源:互联网 发布:巴恩斯数据 编辑:程序博客网 时间:2024/06/04 18:16
一. 题目描述
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 andit 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 tonightwithout alerting the police.
二. 题目分析
动态规划,设置maxV[i]表示到第i个房子位置,最大收益。
递推关系为maxV[i] = max(maxV[i-2]+num[i], maxV[i-1])
注:可能会对上述递推关系产生疑问,是否存在如下可能性,maxV[i-1]并不含num[i-1]?
在这种情况下maxV[i-1]等同于maxV[i-2],因此前者更大。
三. 示例代码
class Solution {public: int rob(vector<int> &num) { int n = num.size(); if(n == 0) return 0; else if(n == 1) return num[0]; else { vector<int> maxV(n, 0); maxV[0] = num[0]; maxV[1] = max(num[0], num[1]); for(int i = 2; i < n; i ++) maxV[i] = max(maxV[i-2]+num[i], maxV[i-1]); return maxV[n-1]; } }};
四. 小结
无
2 0
- leetcode笔记:House Robber
- leetcode笔记-House Robber
- leetcode笔记:House Robber II
- leetcode笔记:House Robber III
- LeetCode笔记:198. House Robber
- 【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
- OC学习第三站
- c++调用ado链接mssql数据库详解
- Hibernate持久化对象的生命周期详解
- 利用Spring的AbstractRoutingDataSource解决多数据源的问题
- nginx php 配置
- leetcode笔记:House Robber
- Styles.Rende @Scripts.Render 错误
- android中dialog封装
- servlet专题
- 音乐播放器分析——目录及总结
- os 模块
- CF#200 div2 C Rational Resistance(math gcd)
- 树状数组详解(处理在线数据,求逆序对)
- 入门篇:高精度乘法