Skip to content

Commit d895f6f

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 213_House_Robber_II.java
1 parent 2eaa83a commit d895f6f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
if (nums == null || nums.length == 0) {
4+
return 0;
5+
}
6+
if (nums.length == 1) {
7+
return nums[0];
8+
}
9+
10+
return Math.max(helper(nums, 0, nums.length - 2), helper(nums, 1, nums.length - 1));
11+
}
12+
13+
private int helper(int[] nums, int start, int end) {
14+
int currHouse = 0, twoHousesAgo = 0, oneHouseAgo = 0;
15+
16+
for (int i = start; i <= end; i++) {
17+
currHouse = Math.max(twoHousesAgo + nums[i], oneHouseAgo);
18+
twoHousesAgo = oneHouseAgo;
19+
oneHouseAgo = currHouse;
20+
}
21+
22+
return currHouse;
23+
}
24+
}

0 commit comments

Comments
 (0)