Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

House Robber

Dynamic Programmingmedium

Implement `rob(nums)` returning the maximum sum you can take without picking two adjacent elements.

Sample tests

Input: rob([1,2,3,1])
Output: 4
Input: rob([2,7,9,3,1])
Output: 12

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Robbing adjacent houses by not tracking the skip case.

Learning resources

  • Wikipedia: Dynamic programming
Approach & explanation (try first)

Rolling the best-including and best-excluding values yields the answer in O(n) time, O(1) space.

Loading...
⌘/Ctrl + Enter

Run your code to see results.