[LeetCode 213] House Roober II
2015-09-10 14:47
477 查看
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.
solution:
similar with house robber I, call that method twice, one not include first element, second not include last element.
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.
solution:
similar with house robber I, call that method twice, one not include first element, second not include last element.
if(nums.length ==1) return nums[0]; int take = 0; int nontake = 0; int maxprofit = 0; for(int i=0;i<nums.length-1;i++){ take = nums[i] + nontake; nontake = maxprofit; maxprofit = Math.max(take, nontake); } take = 0; nontake = 0; int maxprofit1 = 0; for(int i=1;i<nums.length;i++){ take = nums[i] + nontake; nontake = maxprofit1; maxprofit1 = Math.max(take, nontake); } return Math.max(maxprofit, maxprofit1);
相关文章推荐
- 图形用户界面:通过ip获取地址并显示天气情况
- UIButton设置为圆形按钮并增加边框
- JAVA HashMap详细介绍和示例
- 学校类站群网站建设管理
- 获取状态栏高度
- Fragment和Activity
- 很简单的加密算法
- LSTM特点及适用性
- iPhone为什么不设置返回按钮
- lu用户体验_关闭按钮
- MongoDB— 分片技术
- java编程接口(1) ------ Swing基金会
- 【开发者指南】第三章:精灵——学习笔记
- iOS开发 __给文本添加行间距 如Lable的Text
- Javascript面向对象编程(三):非构造函数的继承
- 自定义UITextField背景图片
- perl 卸载mysql数据库
- 使用mvc:interceptors标签来声明需要加入到SpringMVC拦截器链中的拦截器
- chrome插件的导出和导入
- 获取当前应用版本号和系统版本号