File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ // ν΄λΉ λ¬Έμ λ dfsλ‘ νμ΄ νλ λμ€, μκ°μ νμΌλ‘ νμ§ λͺ»νμμ΅λλ€.
2+ // μ΄ν λ΅μ μ°Έκ³ νμκ³ , λ©λͺ¨μ΄μ μ΄μ
μ λ μ¬λ¦¬κ² λμμ΅λλ€.
3+ // νμ΄ μμ΄λμ΄λ iλ²μ§Έ μΈλ±μ€λ₯Ό μ νν ν©κ³Ό i+1λ²μ§Έ μΈλ±μ€λ₯Ό μ νν ν©μ€μ ν° κ²μ μ ννλ κ²μ΄ κ°μ₯ ν° μμ΄λμ΄ μμ΅λλ€.
4+ // κ·Έλ°λ° μ€λ³΅λ κ³Όμ μ΄ μ‘΄μ¬νκΈ°μ λ©λͺ¨μ΄μ μ΄μ
μ νμ©νμ¬
5+ // μλλ O(2^N)μ μκ°λ³΅μ‘λλ₯Ό
6+ // κ° μΈλ±μ€μ ν΄λΉνλ ν¨μνΈμΆμ 1λ²μ©λ§ νλλ‘ μ λνμ¬ μκ°λ³΅μ‘λλ₯Ό ν΄κ²°νμμ΅λλ€.
7+ // μκ°λ³΅μ‘λ : O(N)
8+ // 곡κ°λ³΅μ‘λ : O(N)
9+
10+ class SolutionGotprgmer {
11+ static int [] memo ;
12+ static int answer ;
13+ public int rob (int [] nums ) {
14+ answer = 0 ;
15+ memo = new int [nums .length ];
16+ for (int i =0 ;i <nums .length ;i ++){
17+ memo [i ] = -1 ;
18+ }
19+ return dfs (nums ,0 );
20+ }
21+ public int dfs (int [] nums , int idx ){
22+ if (idx <nums .length ){
23+
24+ if (memo [idx ] != -1 ){
25+ return memo [idx ];
26+ }
27+ else {
28+ memo [idx ] = Math .max (nums [idx ]+dfs (nums ,idx +2 ),dfs (nums ,idx +1 ));
29+ return memo [idx ];
30+ }
31+ }
32+ else {
33+ return 0 ;
34+ }
35+
36+
37+ }
38+ }
You canβt perform that action at this time.
0 commit comments