File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+
You canโt perform that action at this time.
0 commit comments