Search

[198] House Robber

태그
Dynamic Programming
Tier
Medium
날짜
2025/04/10

Question

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.

My Answer

class Solution: def rob(self, nums: List[int]) -> int: if len(nums) < 2: return max(nums) d = [0]*100 d[0] = nums[0] d[1] = max(nums[0],nums[1]) for i in range(2,len(nums)): d[i] = max(d[i-1] ,nums[i]+d[i-2]) return d[len(nums)-1]
Python
복사