LeetCode 213. House Robber II
2016-07-22 01:20
393 查看
213.
House Robber II
My
Submissions QuestionEditorial Solution
Total
Accepted: 27976 Total Submissions: 91078 Difficulty: 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.
这题是上一题House
Robber的升级版~~新加了环形的街道biu biu biu~~
所以就会考虑到,最后一个和第一个房子是不能够同时进入的~要不然会告诉警察叔叔~~
所以分为两种情况~
0.不包括最后一个屋子~就抢劫0~n-2号屋子~
1.不包括第一个屋子~就抢劫1~n-1号屋子~
这样的话,return上面两种情况的最大值就好了~调用两次子函数求值,主函数取其最大值返回~
House Robber II
My
Submissions QuestionEditorial Solution
Total
Accepted: 27976 Total Submissions: 91078 Difficulty: 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.
这题是上一题House
Robber的升级版~~新加了环形的街道biu biu biu~~
所以就会考虑到,最后一个和第一个房子是不能够同时进入的~要不然会告诉警察叔叔~~
所以分为两种情况~
0.不包括最后一个屋子~就抢劫0~n-2号屋子~
1.不包括第一个屋子~就抢劫1~n-1号屋子~
这样的话,return上面两种情况的最大值就好了~调用两次子函数求值,主函数取其最大值返回~
class Solution { public: int rob(vector<int>& nums) { int n = nums.size(); if(n == 0) return 0; if(n == 1) return nums[0]; if(n == 2) return max(nums[0], nums[1]); return max(func(nums, 0, n-2), func(nums, 1, n-1)); } int func(vector<int>& nums, int begin, int end) { int n = end - begin + 1; vector<int> dp(n); dp[0] = nums[begin]; dp[1] = max(nums[begin], nums[begin+1]); for(int i = 2; i < n; i++) { int temp = dp[i - 2] + nums[begin+i]; dp[i] = max(temp, dp[i-1]); } return dp[n - 1]; } };
相关文章推荐
- 207. Course Schedule
- 浅析jQuery整体框架与实现(上)
- Microsoft SQL Server 自定义函数整理大全
- spring配置文件加密(未测试)
- Vasya and String 取尺法
- LeetCode 300. Longest Increasing Subsequence
- Matlab之cell使用
- OSChina 周五乱弹 ——好久不见,你穿上衣服我差点认不出来
- 详解js闭包
- Leetcode Contains Duplicate III
- EventBus 一
- jsp 生成网页验证码的Servlet(未测试)
- LeetCode 304. Range Sum Query 2D – Immutable
- JavaScript 经典实例收集整理
- LeetCode 91. Decode Ways
- LeetCode 279. Perfect Squares
- [React Native] Basic iOS Routing -- NavigatorIOS
- LeetCode 330. Patching Array
- Java 代码实现七牛云存储图片
- 考研路茫茫——空调教室