House Robber && House Robber II

题目: 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做。用dp做的时候脑子里想了这些:

1. state:我们用f(i) 表示在第i户,我们能抢到的最多的钱。

2. function:f(i) = max(f(i - 2) + Mi,  f(i - 1)). 原因是,如果连续强两个,人家就报警了~

3. initialize: f(0) = num[0] , f(1) = max(num[0], num[1]); 在这里我出了bug,没有考虑对于f(1)取最大值,无脑初始化了。

4. return f(1)

 1 public int rob(int[] num) {
 2         if (num == null || num.length == 0) {
 3             return 0;
 4         }
 5         if (num.length == 1) {
 6             return num[0];
 7         }
 8         if (num.length == 2) {
 9             return Math.max(num[0], num[1]);
10         }
11         int[] sum = new int[2];
12         sum[0] = num[0];
13         sum[1] = Math.max(num[1], num[0]);
14         for (int i = 2; i < num.length; i++) {
15             int temp = Math.max(sum[0] + num[i], sum[1]);
16             sum[0] = sum[1];
17             sum[1] = temp;
18         }
19         return sum[1];
20     }

 

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.

思路:

这题是house robber的扩展,就是说房子围成一个圈,首位也是相邻的。假如有1->2->3->4->5。如果抢了1,就不能抢5。因为首尾相连。

那我们既然已经解决了首位不相邻的问题,就相当于,我们已经解决了让一个小偷从0偷到nums.length - 1的问题。

思考这道题的区别。区别就在于,如果偷了头一家,就别偷最后一家,如果没偷头一家,就可以偷最后一家。

换句话说,我们需要知道小偷从0偷到nums.length - 2 和 从1偷到nums.length - 1的问题。 然后返回他两者中的max。

代码如下,helper funciton就做了上题中rob所做的事。

 

 1 public int rob(int[] nums) {
 2         if (nums == null || nums.length == 0) {
 3             return 0;
 4         }
 5         if (nums.length == 1) {
 6             return nums[0];
 7         }
 8         int rob = helper(nums, 0, nums.length - 1);
 9         int notRob = helper(nums, 1, nums.length);
10         return Math.max(rob, notRob);
11     }
12     
13     private static int helper(int[] nums, int start, int end){
14         if (start == end) {
15             return nums[start];
16         }
17         int[] sum = new int[2];
18         sum[0] = 0;
19         sum[1] = 0;
20         for(int i = start; i < end; i++) {
21             int temp = Math.max(sum[0] + nums[i], sum[1]);
22             sum[0] = sum[1];
23             sum[1] = temp;
24         }
25         return sum[1];
26     }

 

转载于:https://www.cnblogs.com/gonuts/p/4395245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值