Skip to content

Commit 37ec25d

Browse files
committed
house-robber solution
1 parent dc7c5e3 commit 37ec25d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

house-robber/rlawjd10.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int rob(vector<int>& nums) {
4+
if (nums.empty()) return 0;
5+
if (nums.size() == 1) return nums[0];
6+
7+
int prev2 = 0; // 두 칸 전 (현재 집 털기)
8+
int prev1 = 0; // 한 칸 전 (현재 집 안털기)
9+
10+
for (int num : nums) {
11+
int current = max(prev1, prev2 + num);
12+
prev2 = prev1;
13+
prev1 = current;
14+
}
15+
16+
return prev1;
17+
}
18+
};
19+

0 commit comments

Comments
 (0)