← Practice · Dynamic programming

House Robber Medium

Houses in a row each hold some money. You cannot rob two adjacent houses. Given an array of amounts, return the maximum total you can rob.

Examples

  • [1,2,3,1] -> 4 (rob houses 1 and 3)
  • [2,7,9,3,1] -> 12

Define a function named rob in JavaScript.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests