[leetcode] House Robber II
2015-06-13 10:45
399 查看
From : https://leetcode.com/problems/house-robber-ii/
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the
first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the
first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
class Solution { public: int find(vector<int>& nums, int start, int end) { int n1=nums[start], n2=0, t; for(int i=start+1; i<=end; i++) { t = n1; n1 = max(n1, n2+nums[i]); n2 = t; } return n1; } int rob(vector<int>& nums) { if(nums.size() == 0) return 0; if(nums.size() == 1) return nums[0]; return max(find(nums, 0, nums.size()-2), find(nums, 1, nums.size()-1)); } };
相关文章推荐
- Java-核心技术总结-第3章
- VM虚拟机中的Ubuntu设置固定IP上网图文教程
- eclipse打开文件所在的本地文件夹
- 初识Struts2框架
- Android获取屏幕分辨率及DisplayMetrics简介
- Java 工厂模式
- Hibernate SQLQuery 原生SQL 查询及返回结果集处理-1
- 模拟面试100问
- phpPHP创建创建jpg格式图片以及压缩图片(转)
- 区间DP poj 2955 hdu 2476
- opencv
- Android Service的生命周期
- 2015061302 - 殷墟
- SDP协议简述
- [C#]浅谈协变与逆变
- hdu 5221 Occupation
- Log4j写入数据库详解
- Counterfeit Dollar
- 去掉.9patch 图的黑线
- 程序员神秘等式(自备纸巾)