forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamcho0608.java
More file actions
35 lines (31 loc) · 1.22 KB
/
samcho0608.java
File metadata and controls
35 lines (31 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// link: https://leetcode.com/problems/house-robber/description/
// difficulty: Medium
class Solution {
// Problem:
// * can't rob two adj houses in the same night
// * return: max amount of money robbale in one night
// Solution:
// * Time Complexity: O(N)
// * Space Complexity: O(N)
public int rob(int[] nums) {
if(nums.length == 1) return nums[0];
if(nums.length == 2) return Math.max(nums[0], nums[1]);
// maxSum[i] = max sum possible with nums[i]
int[] maxSum = new int[nums.length];
maxSum[0] = nums[0];
maxSum[1] = nums[1];
for(int i = 2; i < nums.length; i++) {
if(i == 2) {
maxSum[i] = nums[i] + maxSum[i-2];
continue;
}
// adj houses(i-1) can't be robbed
// choices are:
// 1. i-2 th house (choosing without skipping a house)
// 2. i-3 th house (choosing with skipping a house)
// * choosing < i-4 th houses wouldn't be optimal because it'll be missing either of 1. or 2.
maxSum[i] = nums[i] + Math.max(maxSum[i-2], maxSum[i-3]);
}
return Math.max(maxSum[nums.length-2], maxSum[nums.length-1]);
}
}