动态规划 - 213. House Robber II
URL: https://leetcode.com/problems/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.
解决方案:
Since it is a cyclied array, to make our dynamic programming alogrithm works, we need to break the cyclied array into two single-way arrays.
2-The priciple here is that either a particular house i-th can be robbed or not robbed at all.
For example 1 -> 3 -> 6 -> 5, we can break at the first position, so that the problem is divided to two parts,which are 1 -> 3 -> 6 (we choose to rob the first element, but not the last one.) and 3->6->5. Finally, we can reuse the House Robber I to solve these two sub-problems and pick out the optimal solution.
3-Here some of us may have the confusion that: we can rob house 1, it does not mean that we must rob house 1, whether we rob house 1 is depending on its next value. The correct complement of this statement is that: we should not(must not) rob house 1 ( and similarly, whether to rob the last house depends on the whole broken array.)
class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0) return 0; if(nums.length == 1) return nums[0]; int len = nums.length;
int[] amount = new int[len];
//The first case: we can rob house 1, definitely, we cannot rob the last one
amount[0] = nums[0];
// whether we rob house 1 is depending the relative value
amount[1] = Math.max(nums[0],nums[1]);
for(int idx =2; idx < len-1; idx ++){
amount[idx] = Math.max(amount[idx-1],amount[idx-2] + nums[idx]);
}
// the last element is zero. amount[len-1] by default is zero
int totalSumCaseOne = amount[len-2]; // The second case: we don't rob house 1 at all.
amount = new int[len];
amount[0] = 0;
amount[1] = nums[1];
for(int idx = 2 ;idx< len; idx++){
amount[idx] = Math.max(amount[idx-1],amount[idx-2] + nums[idx]);
}
int totalSumCaseTwo = amount[len-1]; //select the largest one
return Math.max(totalSumCaseOne,totalSumCaseTwo);
}
}
动态规划 - 213. House Robber II的更多相关文章
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 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:二叉树下的不能相邻,求能 ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- 213. House Robber II(动态规划)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 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 ...
- 213. House Robber II
题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...
随机推荐
- gulp入门指南
1. 全局安装 gulp: $ npm install --global gulp 2. 作为项目的开发依赖(devDependencies)安装: $ npm install --save-dev ...
- java io系列18之 CharArrayReader(字符数组输入流)
从本章开始,我们开始对java io中的“字符流”进行学习.首先,要学习的是CharArrayReader.学习时,我们先对CharArrayReader有个大致了解,然后深入了解一下它的源码,最后通 ...
- [时序图笔记] 步步为营UML建模系列五、时序图(Squence diagram)【转】
概述 顺序图是一种详细表示对象之间以及对象与参与者实例之间交互的图,它由一组协作的对象(或参与者实例)以及它们之间可发送的消息组成,它强调消息之间的顺序. 顺序图是一种详细表示对象之间以及对象与系统外 ...
- Java技能
1.思维导图 Java知识思维导图 2 3.Java理解 深入理解Java:注解(Annotation)自定义注解入门 4.JAVA数据类型 基本数据类型 布尔类型:boolean 数值类型: 定点类 ...
- JVM简析
JVM结构图 1.程序计数器 是最小的一块内存区域,它的作用是当前线程所执行的字节码的行号指示器,在虚拟机的模型里,字节码解释器工作时就是通过改变这个计数器的值来选取下一条需要执行的字节码指令,分支. ...
- python socket 编程
TCP IPv4 # server.py import socket import threading import time s = socket.socket(socket.AF_INET,soc ...
- Vue 架构
vue 一.认识Vue 定义:一个构建数据驱动的 web 界面的渐进式框架 优点: 1.可以完全通过客户端浏览器渲染页面,服务器端只提供数据 2.方便构建单页面应用程序(SPA) 二.引入Vue &l ...
- 404.17 - 动态内容通过通配符 MIME 映射映射到静态文件处理程序
刚刚重装了系统,原有的ASP.NET工程下面的WebService无法运行,如下: 404.17 - 动态内容通过通配符 MIME 映射映射到静态文件处理程序 微软的提示,是做三项更改,但是我改了之后 ...
- glide:4.7.1 与 26.1.0冲突
implementation 'com.android.support:support-v4:26.1.0'implementation 'com.github.bumptech.glide:glid ...
- 4.mycat部署
1.准备工作 mycat依赖Java环境,所以必须安装jdk yum install java-1.8.0-openjdk-devel.x86_64 配置JAVA_HOME环境变量 ls -lrt / ...