198. House Robber
题目:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that 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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
链接: http://leetcode.com/problems/house-robber/
题解:
一维DP, 当前最大值相当于Math.max(nums[i - 1],nums[i] + nums[i - 2]),处理一下边界情况就可以了。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
for(int i = 0; i < nums.length; i++){
int temp1 = i - 1 < 0 ? 0 : nums[i - 1];
int temp2 = i - 2 < 0 ? 0 : nums[i - 2];
nums[i] = Math.max(temp1, nums[i] + temp2);
}
return nums[nums.length - 1];
}
}
Update:
public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int prePre = 0, pre = 0;
for(int i = 0; i < nums.length; i++) {
prePre = i - 2 >= 0 ? nums[i - 2] : 0;
pre = i - 1 >= 0 ? nums[i - 1] : 0;
nums[i] = Math.max(nums[i] + prePre, pre);
}
return nums[nums.length - 1];
}
}
Update:
做到了House Rob II,返回来思考,以下写会比较方便,不用更改原数组,用三个变量就可以了。跟Climb Stairs很像
public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int prePre = 0, pre = 0, max = 0;
for(int i = 0; i < nums.length; i++) {
if(i - 2 < 0)
prePre = 0;
if(i - 1 < 0)
pre = 0;
max = Math.max(nums[i] + prePre, pre);
prePre = pre;
pre = max;
}
return max;
}
}
二刷:
dp依然不过关啊...
Java:
更改原数组的.
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int len = nums.length;
for (int i = 0; i < len; i++) {
int notRobLastHouse = i - 2 >= 0 ? nums[i - 2]: 0;
int robLastHouse = i - 1 >= 0 ? nums[i - 1] : 0;
nums[i] = Math.max(robLastHouse, notRobLastHouse + nums[i]);
}
return nums[len - 1];
}
}
分别记录robLast和notRobLast的
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int notRobLastHouse = 0;
int robLastHouse = 0;
for (int i = 0; i < nums.length; i++) {
if (i % 2 == 0) {
notRobLastHouse = Math.max(notRobLastHouse + nums[i], robLastHouse);
} else {
robLastHouse = Math.max(robLastHouse + nums[i], notRobLastHouse);
}
}
return Math.max(notRobLastHouse, robLastHouse);
}
}
类似Climb Stairs的
public class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int prePre = 0, pre = 0, max = 0;
for (int i = 0; i < nums.length; i++) {
max = Math.max(nums[i] + prePre, pre);
prePre = pre;
pre = max;
}
return max;
}
}
三刷:
Java:
dp - O(n) Space, 我们先建立一个长为len + 1的dp数组,dp[i]代表到原数组第i - 1位为止,最大的profit,然后设置dp[1] = nums[0],之后就可以用转移方程比较dp[i - 1]和dp[i - 2] + nums[i - 1]来求得dp[i]。最后返回dp[len]
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int len = nums.length;
int[] dp = new int[len + 1];
dp[1] = nums[0];
for (int i = 2; i <= len; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
}
return dp[len];
}
}
dp O(1) Space : 同样我们可以压缩space complexity到O(1),可以使用和climbing stairs类似的方法,用三个变量来保存临时结果。robLast和notRobLast分别代表 i - 1步和i - 2步。
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int profit = 0, robLast = 0, notRobLast = 0;
for (int i = 0; i < nums.length; i++) {
profit = Math.max(robLast, notRobLast + nums[i]);
notRobLast = robLast;
robLast = profit;
}
return profit;
}
}
Update:
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int robLast = 0, notRobLast = 0, res = 0;
for (int num : nums) {
res = Math.max(notRobLast + num, robLast);
notRobLast = robLast;
robLast = res;
}
return res;
}
}
Reference:
https://leetcode.com/discuss/30079/c-1ms-o-1-space-very-simple-solution
https://leetcode.com/discuss/30020/java-o-n-solution-space-o-1
https://leetcode.com/discuss/31878/java-dp-solution-o-n-runtime-and-o-1-space-with-inline-comment
198. House Robber的更多相关文章
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 198. House Robber(动态规划)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 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] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Leetcode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- (easy)LeetCode 198.House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 【LeetCode】198 - House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java [Leetcode 198]House Robber
题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
随机推荐
- java如何调用webservice接口
java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用.理论上 ...
- 在20上链接db2
首先 db2 connect to CICMDB user ptqs using ptqs; db2进入,出现db2 prep cperftest_bysqlc.sqC bindfile; 就可以sq ...
- L009-oldboy-mysql-dba-lesson09
L009-oldboy-mysql-dba-lesson09 mysql> grant replication salve,replication client on *.* to ‘repl_ ...
- hash桶
#include <stdio.h> #include <stdlib.h> #include "chain.c" //include the chain. ...
- javascript 事件的学习
1.事件绑定: addEventListener , removeEventListener.是dom2级别的事件绑定 attachEvent , detachEvent 是IE的事件绑定. 2. 事 ...
- dbt
Procedure Relocate(s : state; b : base_index) { Move base for state s to a new place beginning at b ...
- Hello World for U (20)
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...
- SharedPreference.Editor的apply与commit方法不同之处
定义: void apply boolean commit; 相同:二者都是提交修改的数据 手机里的文件存放在/data/data/<package_name>/shared_prefs ...
- 五、mysql存储引擎
show variable like 'table_type'; 显示系统默认存储引擎 show engine\G 显示系统支持存储殷勤 =============================== ...
- SRM 616 ColorfulCoins
题意:给定一个从小到大的货币面值,每一个面额都是其前面面额的倍数(倍数大于等于2),每一种货币面值对应一种颜色,目前不清楚面值与颜色的对应关系.要求用最少的查询次数来确定面额与颜色的对应关系.(一次查 ...