File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ - ๋ฌธ์ : https://leetcode.com/problems/house-robber/
2+ - ํ์ด: https://algorithm.jonghoonpark.com/2024/07/03/leetcode-198
3+
4+ ## ๋ด๊ฐ ์์ฑํ ํ์ด
5+
6+ ``` java
7+ public class Solution {
8+ public int rob (int [] nums ) {
9+ int [] dp = new int [nums. length];
10+ int max = 0 ;
11+
12+ if (nums. length == 1 ) {
13+ return nums[0 ];
14+ }
15+
16+ if (nums. length == 2 ) {
17+ return Math . max(nums[0 ], nums[1 ]);
18+ }
19+
20+ if (nums. length == 3 ) {
21+ return Math . max(nums[2 ] + nums[0 ], nums[1 ]);
22+ }
23+
24+ dp[0 ] = nums[0 ];
25+ dp[1 ] = nums[1 ];
26+ dp[2 ] = nums[2 ] + nums[0 ];
27+ max = Math . max(dp[2 ], dp[1 ]);
28+ for (int i = 3 ; i < nums. length; i++ ) {
29+ dp[i] = Math . max(nums[i] + dp[i - 2 ], nums[i] + dp[i - 3 ]);
30+ max = Math . max(max, dp[i]);
31+ }
32+ return max;
33+ }
34+ }
35+ ```
36+
37+ ### TC, SC
38+
39+ ์๊ฐ ๋ณต์ก๋๋ O(n), ๊ณต๊ฐ ๋ณต์ก๋๋ O(n) ์ด๋ค.
You canโt perform that action at this time.
0 commit comments