Skip to content

Commit a41297f

Browse files
donghyeon95donghyeon95
authored andcommitted
feat: House Robber #264
1 parent 27d89c9 commit a41297f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
private int[] dp;
5+
6+
public int rob(int[] nums) {
7+
// ์ ํ™”์‹
8+
// f(x) = f(๋‚˜๋ฅผ ์„ ํƒ) + f(๋‚˜๋ฅผ ์•ˆ์„ ํƒ)
9+
// 100๊ฐœ๋ผ์„œ ๊ฐ€๋Šฅ์€ ํ•  ๊ฑฐ ๊ฐ™๋‹ค.
10+
11+
dp = new int[100];
12+
13+
// 0๋„ ๊ฐ€๋Šฅ ํ•˜๋‹ค
14+
Arrays.fill(dp, -1);
15+
16+
17+
return recurse(nums, 0);
18+
19+
}
20+
21+
public int recurse(int[] nums, int index) {
22+
// ์ข…๋ฃŒ ์กฐ๊ฑด
23+
if (index >= nums.length) return 0;
24+
25+
// ์ด๋ฏธ ํ•œ๋ฒˆ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด
26+
if (dp[index] != -1) return dp[index];
27+
28+
int result = 0;
29+
30+
// ๋‚˜๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ,
31+
result = Math.max(recurse(nums, index+2)+nums[index], result);
32+
33+
// ๋‚˜๋ฅผ ์„ ํƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ,
34+
result = Math.max(recurse(nums, index+1), result);
35+
36+
dp[index] = result;
37+
return result;
38+
}
39+
}
40+

0 commit comments

Comments
ย (0)