File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
src/main/java/sgyj/leetcode/yeji/section6 Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments