Skip to content

Commit a433d65

Browse files
committed
Time: 0 ms (100%), Space: 10.7 MB (45.01%) - LeetHub
1 parent 780c7f0 commit a433d65

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
using ll = long long;
4+
vector<int> dp;
5+
ll solve (vector<int>& v, int i) {
6+
if (i == 0) return v[i];
7+
if (i < 0) return 0;
8+
if (dp[i] != -1) return dp[i];
9+
ll take = v[i] + solve(v, i - 2);
10+
ll leave = solve(v, i - 1);
11+
return dp[i] = max(take, leave);
12+
}
13+
14+
int rob(vector<int>& nums) {
15+
dp.resize(nums.size() + 5, -1);
16+
return solve(nums, nums.size() - 1);
17+
}
18+
};

0 commit comments

Comments
 (0)