House Robber
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 (the robber list cannot be adjacent).
(数组中不相邻数组成的和最大)
Example:
1. 动态规划
为了保证不存在相邻的数,可以维护两个变量表示a_(n-2) 和 a_(n-1),于是就有a_n = max(a_(n-2) + nums[n], a_(n-1))
。具体实现过程如下:
1 | class Solution: |