[Leetcode] House Robber I & II & III 入室盗窃

House Robber I

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.

DP

复杂度

O(N) 时间 O(N) 空间

思路

dp[i] = max{dp[i - 1], dp[i - 2] + value[i]}
抢不抢第i间房子,取决于第i-1间房子抢了没抢:
1 如果第i-1间抢了,那当前(第i间房)就绝不能抢; dp[i] = dp[i - 1]
2 如果第i-1间没抢,那当前(第i间房)可抢可不抢; dp[i] = max(value[i] + dp[i - 2], dp[i - 1])
合并两个得到上面的状态转移方程。

注意

房子的value都是正整数
可以用滚动数组把空间复杂度降低到O(1)

代码

public class Solution {
public int rob(int[] nums) {
if (nums.length == 0)
return 0;
int[] dp = new int[nums.length + 1];
dp[0] = 0;
dp[1] = nums[0];
for (int i = 2; i

House Robber II

Note: This is an extension of House Robber.

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.

DP

复杂度

O(N) 时间 O(N) 空间

思路

这回房子连成环了,言下之意是:抢第一个就不能抢最后一个,不抢第一个抢不抢最后一个随意
两种情况:
1.抢第一间(第二间必须不抢,第三间随意),不抢最后一间(倒数第二间随意)
2.不抢第一间(第二间随意),最后一间抢不抢随意(倒数第二间也随意)
这两种情况分别dp,找最大的就ok了

注意

不抢第一个的情况下,最后一个并不一定要抢,也可以不抢

代码

public class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 0)
return 0;
if (n == 1)
return nums[0];
int[] dpRobFirst = new int[n + 1];
int[] dpNotRobFirst = new int[n + 1];
dpRobFirst[1] = nums[0];
dpRobFirst[2] = dpRobFirst[1];
dpNotRobFirst[1] = 0;
dpNotRobFirst[2] = nums[1];
for (int i = 3; i

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.

分治

复杂度

O(N) 时间 O(N) 空间

思路

树,用分治:
对于当前节点,两种情况:

  1. 抢当前节点,左右子节点必须都不能抢

  2. 不抢当前节点,左右子节点分别可抢可不抢,共四种情况

注意

不抢当前节点有四种情况,别漏

代码

定义一个返回类型,对于每个节点,返回一个Status,里面包括抢/不抢当前节点分别可以得到的最大value

class Status {
int rob;
int notRob;
Status(int rob, int notRob) {
this.rob = rob;
this.notRob = notRob;
}
}
主程序

public class Solution {
public int rob(TreeNode root) {
Status s = helper(root);
return Math.max(s.rob, s.notRob);
}
//given the root node, tell me the maximum money of both rob and not rob current node.
public Status helper(TreeNode root) {
if (root == null)
return new Status(0, 0);
if (root.left == null && root.right == null)
return new Status(root.val, 0);
Status left = helper(root.left);
Status right = helper(root.right);
int robCur = root.val + left.notRob + right.notRob;
int notRobCur = maxOfFour(left.rob + right.rob,
left.rob + right.notRob,
left.notRob + right.rob,
right.notRob + left.notRob);
return new Status(robCur, notRobCur);
}
public int maxOfFour(int a, int b, int c, int d){
int max = Math.max(a, b);
max = Math.max(max, c);
max = Math.max(max, d);
return max;
}
}

关键字:leetcode

版权声明

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部