<LeetCode><Easy> 198 House Robber

来源:互联网 发布:网络会计授课教师招聘 编辑:程序博客网 时间:2024/05/29 02:41

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.


1.递归


#Python2  TimeOut

class Solution(object):    def rob(self, nums):        if nums==[]:            return 0        def maxMoney(ns):            if len(ns)>3:                return max(ns[0]+maxMoney(ns[2:]),ns[1]+maxMoney(ns[3:]))            else:                if len(ns)==3:                    return max(ns[0]+ns[2],ns[1])                elif len(ns)==3:                    return max(ns[0],ns[1])                else:                    return ns[0]        return maxMoney(nums)


0 0
原创粉丝点击