Skip to content

Commit 378a096

Browse files
committed
House Robber
1 parent 898fa6f commit 378a096

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

house-robber/eunhwa99.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
int size = 0;
5+
int[] numArray;
6+
int[] dp;
7+
8+
public int rob(int[] nums) {
9+
size = nums.length;
10+
dp = new int[size];
11+
// 배열의 모든 값을 -1로 변경
12+
Arrays.fill(dp, -1);
13+
numArray = nums;
14+
return fun(0);
15+
}
16+
17+
private int fun(int idx) {
18+
if (idx >= size) return 0;
19+
if (dp[idx] != -1) return dp[idx];
20+
dp[idx] = 0; // check
21+
dp[idx] += Math.max(fun(idx + 2) + numArray[idx], fun(idx + 1));
22+
return dp[idx];
23+
}
24+
}

0 commit comments

Comments
 (0)