描述
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 system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
分析
把问题抽象一下就是:在一个非负的序列中选择一些元素,使得和最大,但不能选择相邻的元素。动态规划问题,公式是 maxv[i] = max(maxv[i-2]+nums[i], maxv[i-1]);
解决方案1(Python)
1 | class Solution(object): |
解决方案2(C++)
1 | class Solution { |
解决方案3(Golang)
1 | func rob(nums []int) int { |
相关问题
- (E) Paint Fence
- (M) House Robber II
- (M) Paint House
- (M) Maximum Product Subarray
- (M) House Robber III