House Robber I & II & III
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.
Given [3, 8, 4]
, return 8
.
分析:
因为“偷”第一个或者“偷”第二个对后面的选择是有影响的,所以从后往前推算更好。
total[i] = Math.max(A[i] + total[i + 2], total[i + 1]);
A[i] + total[i + 2] 指的是偷第i家。
total[i + 1] 指的是不偷i家。
public class Solution {
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
public long houseRobber(int[] A) {
if (A == null || A.length == ) return ;
if (A.length == ) return A[];
if (A.length == ) return Math.max(A[], A[]); long[] total = new long[A.length];
int length = total.length;
total[length - ] = A[length - ];
total[length - ] = Math.max(A[length - ], A[length - ]); for (int i = length - ; i >= ; i--) {
total[i] = Math.max(total[i + ] + A[i], total[i + ]);
}
return total[];
}
}
House Robber II
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.
nums = [3,6,4]
, return 6.
分析:
现在是一个环了,感觉好像找不到起始点。其实反过来想,就是头尾不能同时选而已。所以我们分别选取两个不同的起始点和结束点,跑两次就可以了。
public class Solution { public int houseRobber2(int[] nums) {
if (nums == null || nums.length == ) return ;
if (nums.length == ) return nums[];
return Math.max(getMax(nums, , nums.length - ), getMax(nums, , nums.length - ));
} public int getMax(int[] nums, int start, int end) {
if (start == end) return nums[start];
if (start + == end) return Math.max(nums[start], nums[end]); int[] total = new int[end - start + ];
int m = total.length;
total[m - ] = nums[end];
total[m - ] = Math.max(nums[end], nums[end - ]); for (int i = m - ; i >= ; i--) {
total[i] = Math.max(nums[end - (m - i) + ] + total[i + ], total[i + ]);
}
return total[];
}
}
House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
分析:
看到树,想都不用多想,立马想到递归。关键是这个递归怎么写啊?
既然我们不知道是否把root包含进去是否是最优,那么我们就创建一个函数,返回一个数组,这个数组包含两种情况下的最大值。
public class Solution { public int rob(TreeNode root) {
if(root == null) return ; int[] result = helper(root);
return Math.max(result[], result[]);
} public int[] helper(TreeNode root){
if(root == null){
return new int[]{, };
} int[] result = new int[];
int[] left = helper(root.left);
int[] right = helper(root.right); // result[0] is when root is selected, result[1] is when not.
result[] = root.val + left[] + right[];
result[] = Math.max(left[], left[]) + Math.max(right[], right[]); return result;
}
}
House Robber I & II & III的更多相关文章
- 解题思路:house robber i && ii && iii
这系列题的背景:有个小偷要偷钱,每个屋内都有一定数额的钱,小偷要发家致富在北京买房的话势必要把所有屋子的钱都偷了,但是屋子之内装了警报器,在一定条件下会触发朝阳群众的电话,所以小偷必须聪明一点,才能保 ...
- Leetcode 137. Single Number I/II/III
Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...
- 买卖股票的最佳时机I II III IV
I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- 1. Two Sum I & II & III
1. Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [Locked] Shortest Word Distance I & II & III
Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...
随机推荐
- 冲刺Two之站立会议7
今天我们把软件的基本功能完成之后,又对所有的界面进行了统一规范化并进行了相应的优化.
- 这个C#程序真了不起
(1)在2~31中,这个数不能且仅不能被两个相邻数整除 (2)2 123 581 660 200 (2,3,4,5,6,7,8,9,10,11,12,13,14,15,18,19,20,21,22,2 ...
- Win 2008 r2 远程桌面多用户登陆,一用户多登陆配置
Windows 2008 R2远程桌面,设置最大连接数,一个登录后另一个就被踢掉等问题 Windows 2008 R2配置如图: 1.打开远程桌面回话主机配置 2.右键RDP-Tcp,属性,可设置最大 ...
- 从零开始学Kotlin-操作符(3)
从零开始学Kotlin基础篇系列文章 冒号操作符 ":" 和 "::" :操作符用来定义变量.类的继承等 var name: String//定义变量 clas ...
- [转帖]DRAM芯片战争,跨越40年的生死搏杀
DRAM芯片战争,跨越40年的生死搏杀 超级工程一览 ·2017-08-20 12:50·半导体行业观察 阅读:1.4万 来源:内容来自超级工程一览 , 谢谢. DRAM是动态随机存储器的意思,也就是 ...
- 基于 ARM的 Windows 10S 笔记本 转帖
首款骁龙笔记本华硕畅370评测:续航不俗 性能拖后腿 2018年06月21日 12:23 新浪数码 缩小字体放大字体收藏微博微信分享 相关阅读:国内首款骁龙本华硕畅370发布:6199元送一年无限 ...
- Spring注解开发简要步骤
1.除spring基本包外还需要下载AOP包 spring-aop-4.2.4.RELEASE.jar 2.导入约束(最后两行) <beans xmlns="http://www.sp ...
- hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)
Distinct Values Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- LOJ #2145. 「SHOI2017」分手是祝愿
题目链接 LOJ #2145 题解 一道画风正常的--期望DP? 首先考虑如何以最小步数熄灭所有灯:贪心地从大到小枚举灯,如果它亮着则修改它.可以求出总的最小步数,设为\(cnt\). 然后开始期望D ...
- 【ARC082E】ConvexScore
Description 给定二维直角坐标系上的N个点\((X_i,Y_i)\),定义一个有N个点中的部分点所构成点集为"凸点集",当且仅当该集合内的所有点恰好构成一个面积为正的凸多 ...