House Robber

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Intro DP — Remember Subanswers

Reuse smaller answers via a table or rolling variables.

int a = 1, b = 1;
for (int i = 2; i <= n; i++) {
    int c = a + b;
    a = b; b = c;
}
return b;
LeetCode
2/3
MediumIntro Dynamic ProgrammingLeetCode #198

House Robber

dp[i] = max(dp[i-1], dp[i-2]+nums[i]). Can't rob adjacent.

Examples

Example 1

Input

4
1 2 3 1

Output

4
Example 2

Input

5
2 7 9 3 1

Output

12

Stuck? Take a nudge

C++17