House Robber
Houses stand in a row, each with some money in it. Take as much as you can, but never from two houses side by side: that sets off the alarm. Our example: [6, 2, 1, 7, 4, 3] — at most 16, from 6, 7 and 3.
- input
nums: how much money each house along a street keeps.- returns
- The most money you can take without robbing two houses next to each other: the largest sum of
nums[i]over indices no two of which are neighbours. - constraints
- 1 ≤ nums.length ≤ 100
- 0 ≤ nums[i] ≤ 400
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @return {number}
*/
function rob(nums) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more dynamic programming (dp) problems
- 70Climbing StairsEasysolve
- 121Best Time to Buy and Sell StockEasysolve
- 322Coin ChangeMediumsolve
- 300Longest Increasing SubsequenceMediumsolve
- 62Unique PathsMediumsolve
- 1143Longest Common SubsequenceMediumsolve
- 53Maximum SubarrayMediumsolve
- 918Maximum Sum Circular SubarrayMediumsolve
- 139Word BreakMediumsolve
- 120TriangleMediumsolve
- 64Minimum Path SumMediumsolve
- 63Unique Paths IIMediumsolve
- 97Interleaving StringMediumsolve
- 72Edit DistanceMediumsolve
- 221Maximal SquareMediumsolve
- 123Best Time to Buy and Sell Stock IIIHardsolve
- 188Best Time to Buy and Sell Stock IVHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.