Skip to content

Commit ddd020b

Browse files
committed
house robber
1 parent 3b841e0 commit ddd020b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

β€Žhouse-robber/YuuuuuuYu.javaβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Runtime: 0ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 42.72MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: 두 κ°€μ§€ λ³€μˆ˜λ§Œ μ‚¬μš©ν•œ DP 접근법
9+
* - withoutPrevHouse: 이전 집을 ν„Έμ§€ μ•Šμ•˜μ„ λ•Œμ˜ μ΅œλŒ€ κΈˆμ•‘
10+
* - withPrevHouse: 이전 μ§‘κΉŒμ§€ κ³ λ €ν•œ μ΅œλŒ€ κΈˆμ•‘
11+
* - 각 μ§‘μ—μ„œ "ν„Έκ±°λ‚˜ μ•ˆ ν„Έκ±°λ‚˜" 두 κ°€μ§€ 선택지 쀑 μ΅œλŒ€κ°’ 선택
12+
* 1) ν˜„μž¬ 집을 μ•ˆ ν„Έλ©΄: 이전 μ§‘κΉŒμ§€μ˜ μ΅œλŒ€κ°’ μœ μ§€
13+
* 2) ν˜„μž¬ 집을 ν„Έλ©΄: μ „μ „ μ§‘κΉŒμ§€μ˜ μ΅œλŒ€κ°’ + ν˜„μž¬ μ§‘ κΈˆμ•‘
14+
*/
15+
class Solution {
16+
public int rob(int[] nums) {
17+
int withoutPrevHouse = 0;
18+
int withPrevHouse = 0;
19+
20+
for (int money: nums) {
21+
int maxMoney = Math.max(withPrevHouse, withoutPrevHouse+money);
22+
withoutPrevHouse = withPrevHouse;
23+
withPrevHouse = maxMoney;
24+
}
25+
26+
return withPrevHouse;
27+
}
28+
}

0 commit comments

Comments
Β (0)