Leetcode 213.大家劫舍II
打家劫舍II
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。
示例 1:
输入: [2,3,2]
输出: 3
解释: 你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
示例 2:
输入: [1,2,3,1]
输出: 4
解释: 你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
基本思路:
第一个房屋和最后一个房屋是紧挨着的,说明第一个房屋和最后一个房屋不能同时盗取。我们可以考虑两种情况:
(1)考虑偷取[0, n - 2]的房屋。
(2)考虑偷取[1, n - 1]的房屋。
取上述两种情况的大者即为答案。
class Solution {
public int rob(int[] nums) {
if(nums.length==0) return 0;
if(nums.length==1) return nums[0];
int[] leftNums=new int[nums.length-1];
int[] rightNums=new int[nums.length-1];
for(int i=0;i<nums.length-1;i++){
leftNums[i]=nums[i];
}
for(int i=1;i<nums.length;i++){
rightNums[i-1]=nums[i];
}
int left=kRob(leftNums);
int right=kRob(rightNums);
return left>right?left:right;
}
public int kRob(int[] nums) {
int n=nums.length;
if(n==0) return 0;
if(n==1) return nums[0];
int[] dp=new int[n];
dp[0]=nums[0];
dp[1]=nums[1];
if(dp[1]<dp[0])dp[1]=dp[0];
for(int i=2;i<n;i++){
dp[i]=dp[i-2]+nums[i];
if(dp[i]<dp[i-1]) dp[i]=dp[i-1];
}
return dp[n-1];
}
}
Leetcode 213.大家劫舍II的更多相关文章
- 【LeetCode 213】House Robber II
This is an extension of House Robber. After robbing those houses on that street, the thief has found ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- Leetcode:面试题68 - II. 二叉树的最近公共祖先
Leetcode:面试题68 - II. 二叉树的最近公共祖先 Leetcode:面试题68 - II. 二叉树的最近公共祖先 Talk is cheap . Show me the code . / ...
- Leetcode:面试题55 - II. 平衡二叉树
Leetcode:面试题55 - II. 平衡二叉树 Leetcode:面试题55 - II. 平衡二叉树 Talk is cheap . Show me the code . /** * Defin ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- 51nod 1134最长递增子序列
1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素 ...
- 大小写 unix and windows
如果你没有使用工具, 只是sqlplus对大小写不敏感. 如果你要给sql传递参数,且在windows下面就不需要考虑.如果是aix系统,最好写一样.
- 438 Find All Anagrams in a String 找出字符串中所有的变位词
详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...
- JavaScript - try catch finally throw
语法: try { tryCode - 尝试执行代码块 } catch(err) { catchCode - 捕获错误的代码块 } finally { finallyCode - 无论 try / c ...
- CAS4.0 server 环境的搭建
1.上cas的官网下载cas server 官网地址:https://github.com/Jasig/cas/releases,下载好后 解压下载的 cas-server-4.0.0-release ...
- hexo_config.yml配置内容
# Hexo Configuration ## Docs: https://hexo.io/docs/configuration.html ## Source: https://github.com/ ...
- Mac eclipse java6环境安装
由于旧版adtbundle eclipse需要java se6版本支持,而较新版本mac系统默认安装较高的java版本,所以这里需要卸载高版本jdk(1.8),然后安装1.6 mac删除jdk jav ...
- (转)淘淘商城系列——使用maven tomcat插件启动聚合工程
http://blog.csdn.net/yerenyuan_pku/article/details/72672389 上文我们一起学习了如何使用maven tomcat插件来启动web工程,本文我们 ...
- listener.log文件过大导致oracle假死
/home/u01/oracle/product/11gr2/db_1/log/diag/tnslsnr/VM_179_95_centos/listener/trace/listener.log li ...
- Android突破64K限制
1.添加依赖 android{ defaultConfig{ ... multiDexEnabled true ... } } dependencies{ compile 'com.android.s ...