Leetcode 213. House Robber II
2016-06-17 11:23
423 查看
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.
本题在 House Robber 的基础上加上了首尾两间房子相连这一条件。为了运用 House Robber 的解题方法,将此题分解为两种情况:[0,n-2] 和 [1,n-1] , 在这两种情况中选较大者,这两种情况每种的解法和 House Robber 方法一致(DP)。
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.
本题在 House Robber 的基础上加上了首尾两间房子相连这一条件。为了运用 House Robber 的解题方法,将此题分解为两种情况:[0,n-2] 和 [1,n-1] , 在这两种情况中选较大者,这两种情况每种的解法和 House Robber 方法一致(DP)。
class Solution { public: int rob(vector<int>& nums) { int n = nums.size(); if (n < 2) return n ? nums[0] : 0; return max(robber(nums, 0, n - 2), robber(nums, 1, n - 1)); } private: int robber(vector<int>& nums, int l, int r) { int pre = 0, cur = 0; for (int i = l; i <= r; i++) { int temp = max(pre + nums[i], cur); pre = cur; cur = temp; } return cur; } };
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题