198. House Robber [easy] (Python)

原创 2016年06月01日 14:39:22

题目链接

https://leetcode.com/problems/house-robber/

题目原文

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.

题目翻译

你要去一条街上偷钱,每一户人家都有一定量的钱,但是你不能偷相邻两家的,因为相邻的两家有安保系统相连,偷了你就被警察叔叔带走了(什么鬼安保系统= =)。
给定一个非负数组,代表每家有多少钱。求出在不被警察叔叔带走的情况下,你今晚最多能偷多少钱。

思路方法

思路一

这个问题可以这么想,假设只有一家,那么你只能偷这家;假设有两家,那么你要判断两家哪个钱多,偷哪个;依次类推,假设有n家,那么你要判断“偷第n家不偷第n-1家且前n-2家尽量多的偷”和“不偷第n家且前n-1家尽量多的偷”,哪个得到的钱多偷哪个。
你可以递归求解,然而复杂度太高无法AC。所以应该记录已经计算过的结果,于是这变成一个动态规划问题。

代码

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 0:
            return 0
        elif len(nums) < 2:
            return max(nums[0], nums[-1])
        money = [0]*len(nums)
        money[0], money[1] = nums[0], max(nums[0], nums[1])
        for i in xrange(2, len(nums)):
            money[i] = max(nums[i] + money[i-2], money[i-1])
        return money[len(nums)-1]

思路二

当然你会发现,上面的代码使用的空间是冗余的,因为每次循环只会用到前两个数据。所以代码可以降低空间复杂度到O(1)。

代码

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        now = last = 0
        for i in nums:
            last, now = now, max(i+last, now)
        return now

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51555813

版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode 337. House Robber III-动态规划|Java|Python简洁高效

原题链接:337. House Robber III 【思路】 和 House Robber 十分类似,是Dynamic Programming问题。这里使用递归来实现,数组rob来存储。rob[0...

leetCode198-House Robber

链接:https://leetcode.com/problems/house-robber/ 这道理可以看做是状态压缩,每两个数字看做是一行,状态有3个,故需要F[N][3]的数组,F[i][j]就表...

198. House Robber Leetcode Python

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

Leetcode_198_House Robber

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47680663 You are a profes...

198 House Robber

题目链接:https://leetcode.com/problems/house-robber/题目:You are a professional robber planning to rob hou...

198. House Robber(C++实现)

动态规划求解,设n-2座房子最多能抢到f(n-2),n-1座房子能最多抢到f(n-1),n座房子最多能抢f(n),最后一座房子里有nMoney, 则可以推导出他们之间的关系(分两种情况,第一种不抢最后...

LeetCode#198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

leetcode 198 & 213:House Robber

一.House Robber I 的题意是:有一排商铺,强盗不能连续抢劫相邻的两家,否则会触动报警器,求强盗在不触动报警器的前提下,可以抢劫的最多金钱总和。(输入一维数组,从中选择若干个数,使得这些数...

198. House Robber

leetcode 198 - Easy

LeetCode动态规划198. House Robber思路解析

LeetCode动态规划198. House Robber思路解析,用最容易理解的递归解释
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:198. House Robber [easy] (Python)
举报原因:
原因补充:

(最多只允许输入30个字)