func rob(_ nums: [Int]) -> Int {
if nums.isEmpty {
return 0
}
var dp = Array(repeating: 0, count: nums.count+1)
dp[1] = nums[0]
for i in stride(from: 1, to: nums.count, by: 1) {
dp[i+1] = max(dp[i-1] + nums[i], dp[i])
}
return dp[nums.count]
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme