You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. The security system is set up such that you cannot rob two adjacent houses. You need to determine the maximum amount of money you can steal without alerting the security system.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without robbing two adjacent houses.
Input: nums = [2,3,2]
Output: 4
Explanation: Rob house 1 (money = 2) and then rob house 3 (money = 2).
Total amount robbed = 2 + 2 = 4.
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1), rob house 3 (money = 3).
Total amount robbed = 1 + 3 = 4.
n == nums.length0 <= n <= 1000 <= nums[i] <= 400`python def rob(nums): if not nums: return 0 if len(nums) <= 2: return max(nums)
n = len(nums)
dp = [0] * n
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i in range(2, n):
dp[i] = max(dp[i-1], dp[i-2] + nums[i])
return dp[-1]
`
This solution uses dynamic programming to store the maximum amount of money that can be robbed considering the current house and the previous house. It iterates through the array and updates the dp array accordingly. The final answer is stored in dp[-1].