Skip to content
advanced

Rob the street without touching neighbours

Complete the two-number recurrence behind house robber — the best through the previous house and the best through the one before it.

/**
 * Returns the maximum loot obtainable without robbing two adjacent houses.
 * Empty input returns 0.
 */
export function rob(houses: number[]): number {
  // Best that includes the previous house, best that skips it.
  let ________ = 0
  let __________... = 0

  for (const ____ of houses) {
    // Rob this house (so the previous must be skipped) or let it be.
    const current = Math.max(previous, beforePrevious + loot)
    beforePrevious = previous
    previous = current
  }

  return previous
}
0/3 filled tab moves · Ctrl+↵ submits · Ctrl+Shift+↵ runsstarter code