Leetcode 213. House Robber II (Medium) (cpp)

原创 2016年08月28日 21:49:40

Leetcode 213. House Robber II (Medium) (cpp)

Tag: Dynamic Programming

Difficulty: Medium


/*

213. House Robber II (Medium)

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 rob(vector<int>& nums) {
		int _size = nums.size();
		if (_size < 2) return _size ? nums[0] : 0;
		return max(robber(nums, 0, _size - 2), robber(nums, 1, _size - 1));
	}
	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;
	}
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

<LeetCode OJ> (198 / 213) House Robber(I / II)

House Robber My Submissions Question Total Accepted: 45702 Total Submissions: 142460 Difficulty:...

[leetcode 213]House Robber II

Note: This is an extension of House Robber. After robbing those houses on that street, the thief ha...

leetcode - 198,213. House Robber(II) & 91. Decode Ways

算法系列博客之Dynamic Programming 本篇博客将运用动态规划的思想来解决leetcode上198和213号问题 问题描述:198 House RobberYou are a pr...
  • Quiteen
  • Quiteen
  • 2017年06月11日 15:09
  • 111

LeetCode - 213. House Robber II

这道题目是House Robber的升级版,在这一题中,房子首尾相连,所以头部的房子和尾部的房子不能同时抢劫,这道题目只是比上道House Robber题目多了这一个限制条件,在上道题目中,第一个房子...

LeetCode-213. House Robber II (JAVA)(有环)

LeetCode-213. House Robber II (JAVA)(有环)

Leetcode213-House Robber II

After robbing those houses on that street, the thief has found himself a new place for his thievery ...

LeetCode 213. House Robber II

213. House Robber IIDescription After robbing those houses on that street, the thief has found hims...

LeetCode 213:House Robber II

class Solution { public: int rob(vector& nums) { int length = nums.size(); //cout

LeetCode 213. House Robber II 注意考察环尾和环头交叉处

213. House Robber II After robbing those houses on that street, the thief has found himself a new ...

Leetcode 213 House Robber II 抢劫最大金额

HouseRobber扩展:抢劫完那条街道上的房子之后,这个贼有找到了实施自己盗窃行动的另一个地点,这样他才不会引来太多注意。这次,这里的搜有房子排成了一个圆圈,这意味着第一个房子和最后一个房子也是挨...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode 213. House Robber II (Medium) (cpp)
举报原因:
原因补充:

(最多只允许输入30个字)