关键词:
问题
非负数组表示每个房子拥有的金钱,抢了一个房子后不能抢隔壁房子的,求能抢到的最大数额。
Input: [1,2,3,1]
Output: 4
Explanation: 1 + 3 = 4.
思路
用dp[i]表示以index i结尾的子数组里,能抢的金钱最大和(不一定要抢nums[i])。可以得到dp公式为:(dp[i] = max(dp[i-1], dp[i-2] + nums[i]))。
因为dp只取决于前两个数,可以用两个变量f1和f2优化,省去dp的数组空间开销。
- 时间复杂度O(n),空间复杂度O(1)
代码
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if(len(nums) == 0):
return 0
elif(len(nums) == 1):
return nums[0]
f2 = nums[0]
f1 = max(nums[0], nums[1])
for i in range(2,len(nums)):
f1, f2 = max(f2+nums[i], f1), f1
return f1
c_cpp198.houserobber(代码片段)
java198.houserobber(o(1)space).java(代码片段)
java198.houserobber(o(1)space).java(代码片段)
java198.houserobber(o(1)space).java(代码片段)
java198.houserobber(o(1)space).java(代码片段)
java198.houserobber(o(1)space).java(代码片段)
java198.houserobber(o(1)space).java(代码片段)
198.houserobber(代码片段)
Youareaprofessionalrobberplanningtorobhousesalongastreet.Eachhousehasacertainamountofmoneystashed,theonlyconstraintstoppingyoufromrobbingeachofthemisthatadjacenthouseshavesecuritysystemconnectedand itwillautomaticallycontactthepoliceiftwoadjacenthouseswerebrokenintoonthesamenight.Givenalistofno... 查看详情
198.houserobber(代码片段)
一、题目 1、审题 2、分析 给出一个整形数组,你不能获取连续元素,只能间隔获取元素的值,求你能获取的元素的和的最大值为多大。 二、解答 1、思路: 方法一、 采用两个变量max... 查看详情
198.houserobber(代码片段)
问题非负数组表示每个房子拥有的金钱,抢了一个房子后不能抢隔壁房子的,求能抢到的最大数额。Input:[1,2,3,1]Output:4Explanation:1+3=4.思路用dp[i]表示以indexi结尾的子数组里,能抢的金钱最大和(不一定要抢nums[i])。可以得到dp公... 查看详情
[lc]198.houserobber(代码片段)
Youareaprofessionalrobberplanningtorobhousesalongastreet.Eachhousehasacertainamountofmoneystashed,theonlyconstraintstoppingyoufromrobbingeachofthemisthatadjacenthouseshavesecuritysystemconnectedand itwillautomaticallycontactthepoliceiftwoadjacenthouseswerebrokenintoonthesamenight.Givenalistofno... 查看详情
leetcode-easy-dynamic-198houserobber(代码片段)
mycode思路:a:123456789 f(9)=max(f(7)+a9,f(8))前一步、前两步至于前三步f(9)=f(6)+a9,但其实f(7)在求值的时候按照上面的公式一定是比f(7)大于等于的,所以f(6)+a9总是小于等于上面的递推式的至于前四步... 查看详情
刷题198.houserobber(代码片段)
一、题目说明题目198.HouseRobber,给一列正整数表示每个房间存的金币,不能连续抢2个房间,计算可以得到的最大金币。二、我的解答这个题目,我列举了n=1,2,3,...5的情况,没有找到规律。后面看了解答知道了:dp[i+1]=max(dp[i-2]+n... 查看详情
198.houserobber
problem198. HouseRobber 1.Leetcode_HouseRobber; 查看详情
198.houserobber
Problemstatment:Youareaprofessionalrobberplanningtorobhousesalongastreet.Eachhousehasacertainamountofmoneystashed,theonlyconstraintstoppingyoufromrobbingeachofthemisthatadjacenthouseshavesecuritysyste 查看详情
198.houserobber
Youareaprofessionalrobberplanningtorobhousesalongastreet.Eachhousehasacertainamountofmoneystashed,theonlyconstraintstoppingyoufromrobbingeachofthemisthatadjacenthouseshavesecuritysystemconnectedand&nb 查看详情
198.houserobber
Youareaprofessionalrobberplanningtorobhousesalongastreet.Eachhousehasacertainamountofmoneystashed,theonlyconstraintstoppingyoufromrobbingeachofthemisthatadjacenthouseshavesecuritysystemconnectedand&nb 查看详情
198.houserobber
Youareaprofessionalrobberplanningtorobhousesalongastreet.Eachhousehasacertainamountofmoneystashed,theonlyconstraintstoppingyoufromrobbingeachofthemisthatadjacenthouseshavesecuritysystemconnectedand&nb 查看详情