House Robber - Problem
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,7,9,3,1]
›
Output:
12
💡 Note:
Rob houses 0, 2, and 4: 2 + 9 + 1 = 12. Cannot rob adjacent houses 1 and 3.
Example 2 — Choose Larger Adjacent
$
Input:
nums = [1,2,3,1]
›
Output:
4
💡 Note:
Rob houses 0 and 2: 1 + 3 = 4, or rob houses 1 and 3: 2 + 1 = 3. The optimal choice is 4.
Example 3 — Single House
$
Input:
nums = [5]
›
Output:
5
💡 Note:
Only one house available, rob it for maximum of 5.
Constraints
- 1 ≤ nums.length ≤ 100
- 0 ≤ nums[i] ≤ 400
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code