leetcode——213. 打家劫舍 II
发布时间:2020-12-20 10:17:30 所属栏目:Python 来源:网络整理
导读:不偷第一家的最大利润p1 不偷最后一家的最大利润是p2 输出p1,p2的最大值 class Solution: def rob(self,nums) - int: if len(nums)== 0: return 0 if len(nums)2 : return nums[0] if len(nums)==2 : return max(nums) else : res = {} res[0] = nums[0] res
不偷第一家的最大利润p1 不偷最后一家的最大利润是p2 输出p1,p2的最大值 class Solution: def rob(self,nums) -> int: if len(nums)==0: return 0 if len(nums)<2: return nums[0] if len(nums)==2: return max(nums) else: res={} res[0]=nums[0] res[1]=max(nums[:2]) for i in range(2,len(nums)): res[i]=max(res[i-2]+nums[i],res[i-1]) nums1=nums[1:] res1={} res1[0]=nums1[0] res1[1]=max(nums1[:2]) for i in range(2,len(nums1)): res1[i]=max(res1[i-2]+nums1[i],res1[i-1]) return max(res[len(nums)-2],res1[len(nums1)-1])
执行用时 :48 ms,在所有?python3?提交中击败了74.29%的用户
内存消耗 :13.8 MB,在所有?python3?提交中击败了5.39%的用户
?
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ——2019.10.16
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |