Skip to content

Commit 7f67f28

Browse files
committed
feat: house-robber-ii
1 parent 6f1a70f commit 7f67f28

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

house-robber-ii/minji-go.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/house-robber-ii/">week14-3. house-robber-ii</a>
3+
* <li>Description: Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police</li>
4+
* <li>Topics: Array, Dynamic Programming </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 40.6MB</li>
7+
*/
8+
9+
class Solution {
10+
public int rob(int[] nums) {
11+
int n = nums.length;
12+
if (n == 1) return nums[0];
13+
if (n == 2) return Math.max(nums[0], nums[1]);
14+
15+
return Math.max(rob(nums, 0, n - 2), rob(nums, 1, n - 1));
16+
}
17+
18+
public int rob(int[] nums, int start, int end) {
19+
int prev1 = 0, prev2 = 0;
20+
for (int i = start; i <= end; i++) {
21+
int temp = Math.max(prev2, prev1 + nums[i]);
22+
prev1 = prev2;
23+
prev2 = temp;
24+
}
25+
return prev2;
26+
}
27+
}

0 commit comments

Comments
 (0)