|
1 | 1 | package com.fishercoder.solutions;
|
2 |
| -/**213. House Robber II |
| 2 | +/** |
| 3 | + * 213. House Robber II |
3 | 4 |
|
4 | 5 | Note: This is an extension of House Robber.
|
5 | 6 |
|
6 |
| -After robbing those houses on that street, |
| 7 | + After robbing those houses on that street, |
7 | 8 | the thief has found himself a new place for his thievery
|
8 | 9 | so that he will not get too much attention.
|
9 | 10 | This time, all houses at this place are arranged in a circle.
|
10 | 11 | That means the first house is the neighbor of the last one.
|
11 | 12 | Meanwhile, the security system for these houses remain the same as for those in the previous street.
|
12 | 13 |
|
13 |
| -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. |
| 14 | +Given a list of non-negative integers representing the amount of money of each house, |
| 15 | + determine the maximum amount of money you can rob tonight without alerting the police. |
14 | 16 | */
|
15 | 17 | public class _213 {
|
| 18 | + public static class Solution1 { |
| 19 | + /** |
| 20 | + * Another dp problem: |
| 21 | + * separate them into two cases: |
| 22 | + * 1. rob from house 1 to n-1, get max1 |
| 23 | + * 2. rob from house 2 to n, get max2 take the max from the above two max |
| 24 | + */ |
| 25 | + public int rob(int[] nums) { |
| 26 | + if (nums == null || nums.length == 0) { |
| 27 | + return 0; |
| 28 | + } |
| 29 | + if (nums.length == 1) { |
| 30 | + return nums[0]; |
| 31 | + } |
| 32 | + if (nums.length == 2) { |
| 33 | + return Math.max(nums[0], nums[1]); |
| 34 | + } |
| 35 | + int[] dp = new int[nums.length - 1]; |
16 | 36 |
|
17 |
| - /**Another dp problem: |
18 |
| - * separate them into two cases: |
19 |
| - * 1. rob from house 1 to n-1, get max1 |
20 |
| - * 2. rob from house 2 to n, get max2 |
21 |
| - * take the max from the above two max*/ |
22 |
| - public int rob(int[] nums) { |
23 |
| - if (nums == null || nums.length == 0) { |
24 |
| - return 0; |
25 |
| - } |
26 |
| - if (nums.length == 1) { |
27 |
| - return nums[0]; |
28 |
| - } |
29 |
| - if (nums.length == 2) { |
30 |
| - return Math.max(nums[0], nums[1]); |
31 |
| - } |
32 |
| - int[] dp = new int[nums.length - 1]; |
| 37 | + //rob 1 to n-1 |
| 38 | + dp[0] = nums[0]; |
| 39 | + dp[1] = Math.max(nums[0], nums[1]); |
| 40 | + for (int i = 2; i < nums.length - 1; i++) { |
| 41 | + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); |
| 42 | + } |
| 43 | + int prevMax = dp[nums.length - 2]; |
33 | 44 |
|
34 |
| - //rob 1 to n-1 |
35 |
| - dp[0] = nums[0]; |
36 |
| - dp[1] = Math.max(nums[0], nums[1]); |
37 |
| - for (int i = 2; i < nums.length - 1; i++) { |
38 |
| - dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); |
| 45 | + //rob 2 to n |
| 46 | + dp = new int[nums.length - 1]; |
| 47 | + dp[0] = nums[1]; |
| 48 | + dp[1] = Math.max(nums[1], nums[2]); |
| 49 | + for (int i = 3; i < nums.length; i++) { |
| 50 | + dp[i - 1] = Math.max(dp[i - 3] + nums[i], dp[i - 2]); |
| 51 | + } |
| 52 | + return Math.max(prevMax, dp[nums.length - 2]); |
39 | 53 | }
|
40 |
| - int prevMax = dp[nums.length - 2]; |
41 |
| - |
42 |
| - //rob 2 to n |
43 |
| - dp = new int[nums.length - 1]; |
44 |
| - dp[0] = nums[1]; |
45 |
| - dp[1] = Math.max(nums[1], nums[2]); |
46 |
| - for (int i = 3; i < nums.length; i++) { |
47 |
| - dp[i - 1] = Math.max(dp[i - 3] + nums[i], dp[i - 2]); |
48 |
| - } |
49 |
| - return Math.max(prevMax, dp[nums.length - 2]); |
50 | 54 | }
|
51 |
| - |
52 | 55 | }
|
0 commit comments