Skip to content

Commit c033942

Browse files
committed
house-robber solved
1 parent a63ea90 commit c033942

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

house-robber/mintheon.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
int[] sums = new int[nums.length];
4+
5+
sums[0] = nums[0];
6+
7+
if (nums.length > 1) {
8+
sums[1] = nums[1];
9+
}
10+
11+
if (nums.length > 2) {
12+
sums[2] = nums[0] + nums[2];
13+
}
14+
15+
if (nums.length > 3) {
16+
for (int i = 3; i < nums.length; i++) {
17+
sums[i] = Math.max(nums[i] + sums[i - 2], nums[i] + sums[i - 3]);
18+
}
19+
}
20+
21+
int max = 0;
22+
for(int sum : sums) {
23+
max = Math.max(sum, max);
24+
}
25+
26+
return max;
27+
}
28+
}

0 commit comments

Comments
 (0)