1. House Robber II

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

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.

Example 1:

Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.

Example 2:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

第一家和最后一家不能同时打劫,因此分别去掉第一家和最后一家,计算能够抢到的最大值

class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)return 0;
if(nums.size() == 1)return nums[0];
if(nums.size() == 2)return max(nums[0], nums[1]);
return max(rob(nums, 0, nums.size() - 1),rob(nums, 1, nums.size()));
}
int rob(vector<int> &nums, int left, int right){
vector<int>dp(nums.size(), 0);
dp[left] = nums[left];
dp[left+1] = max(nums[left], nums[left + 1]);
for(int i = left + 2; i < right; ++i){
dp[i] = max(dp[i-1], dp[i-2] + nums[i]);
}
return dp[right-1];
}
};

【刷题-LeetCode】213. House Robber II的更多相关文章

  1. [LeetCode] 213. House Robber II 打家劫舍 II

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

  2. [LeetCode] 213. House Robber II 打家劫舍之二

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  3. [leetcode] #213 House Robber II Medium (medium)

    原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...

  4. Java for LeetCode 213 House Robber II

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

  5. LeetCode 213. House Robber II

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

  6. 【刷题-LeetCode】275. H-Index II

    H-Index II Given an array of citations sorted in ascending order (each citation is a non-negative in ...

  7. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  8. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  9. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

随机推荐

  1. CF1506A Strange Table 题解

    Content 给定一个 \(n\times m\) 的矩阵.一开始,\((1,1)\) 所在位置上面的数是 \(1\),随后先由上往下将这一列中的所有位置上面填上 \(2,3,\dots,n\),再 ...

  2. Python sys模块 os模块、OS.open() | open() | OS._exit() | sys.exit() | exit()

    sys模块:负责程序和Python交互. sys常用方法:===========================  sys.stdout.write('please:')val = sys.stdin ...

  3. Coder 投稿 | mPaaS 的多版本接入(Android)

    本文作者:mPaaS 用户「Q-Coder」 同时欢迎更多的开发者向 mPaaS 投稿 原文:blog.csdn.net/yqq577/article/details/116801705 前言 对于 ...

  4. jquery autocomplete 插件的使用

    Autocomplete 自动完成 功能:输入框在输入关键字后,根据输入的内容给出相关的下拉框供用户选择,自动完成输入内容. 插件:使用jqueryUI的自动完成小部件,文档地址:https://jq ...

  5. c++设计模式概述之命令

    代码写的不够规范,目的是为了缩短文章篇幅,实际中请不要这样做.  1.概述 命令模式是一种数据驱动的模式.将请求封装到命令的对象中,再传给调用对象,调用对象再处理该命令. [将一个请求封装为一个对象] ...

  6. 魔法串(hud4545)

    魔法串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...

  7. git安装与使用,未完待续... ...

    ​ 目录 一.git概念 二.git简史 三.git的安装 四.git结构 五.代码托管中心-本地库和远程库的交互方式 六.初始化本地仓库 七.git常用命令 1.add和commit命令 2.sta ...

  8. Adversarial Self-Supervised Contrastive Learning

    目录 概 主要内容 Linear Part 代码 Kim M., Tack J. & Hwang S. Adversarial Self-Supervised Contrastive Lear ...

  9. <数据结构>XDOJ317.输出完全二叉树的某一层

    问题与解答 问题描述 对一棵完全二叉树,输出某一深度的所有节点,有则输出这些节点,无则输出EMPTY. 输入格式 输入有多组数据. 每组数据第一行输入一个结点数n(1<=n<=1000), ...

  10. N-Empress

    全排列 基本思想:递归.散列 代码实现 #include<cstdio> const int maxn = 11; int n, P[maxn], hashTable[11] = {fal ...