MediumArrayDynamic Programming

House Robber

LeetCode
1 approach, code in all languages

A row of houses each hold a certain amount of money, given in an integer array nums. A security system links every pair of directly adjacent houses, so robbing two neighboring houses on the same night triggers an alarm.

Return the largest total amount of money you can steal in one night without ever robbing two adjacent houses.

Example 1

Input: nums = [1,2,3,1]

Output: 4

Rob house 0 (money = 1) and house 2 (money = 3) for a total of 1 + 3 = 4.

Example 2

Input: nums = [2,7,9,3,1]

Output: 12

Rob houses 0, 2, and 4 (2 + 9 + 1 = 12); no two chosen houses are adjacent.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 400
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now