Skip to content

Commit e1429fc

Browse files
committed
feat : leetCode DP House Robber 문제풀이
1 parent 465a5bf commit e1429fc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sgyj.leetcode.yeji.section6;
2+
3+
// House Robber
4+
public class Solution198 {
5+
private static int[] stillMoney;
6+
7+
public static int rob(int[] nums) {
8+
int answer = 0;
9+
stillMoney = new int[nums.length];
10+
for(int n=0; n<nums.length; n++){
11+
stillMoney[n] = nums[n];
12+
}
13+
14+
for(int n=2; n<nums.length; n++){
15+
int max = 0;
16+
for(int i=n-2; i>=0; i--){
17+
max= Math.max(max,stillMoney[i]);
18+
}
19+
stillMoney[n]+=max;
20+
}
21+
22+
for(int money:stillMoney){
23+
answer = Math.max(answer,money);
24+
}
25+
26+
27+
return answer;
28+
}
29+
30+
public static void main ( String[] args ) {
31+
int[] nums = {1,2,3,1};
32+
33+
System.out.println(rob(nums));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)