zoukankan      html  css  js  c++  java
  • leetcode198 House Robber

     1 """
     2 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.
     3 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.
     4 Example 1:
     5 Input: [1,2,3,1]
     6 Output: 4
     7 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
     8              Total amount you can rob = 1 + 3 = 4.
     9 Example 2:
    10 Input: [2,7,9,3,1]
    11 Output: 12
    12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
    13              Total amount you can rob = 2 + 9 + 1 = 12.
    14 """
    15 """
    16 看到这道题,把问题想简单了
    17 思路是 奇数列求和 ,偶数列求和,取最大的值。
    18 其实不然。对于下面这个用例是 wrong answer
    19 Input
    20 [2,1,1,2]
    21 Output
    22 3
    23 Expected
    24 4
    25 """
    26 class Solution1(object):    #Wrong Answer
    27     def rob(self, nums):
    28         n = len(nums)
    29         if n%2 == 0:
    30             even, odd = 0, 0
    31             for i in range(0, n, 2):
    32                 even += nums[i]
    33                 odd += nums[i+1]
    34         else:
    35             even, odd = nums[0], 0
    36             for i in range(1, n, 2):
    37                 odd += nums[i]
    38                 even += nums[i+1]
    39         return max(even, odd)
    40 
    41 """
    42 下面是正确的思路。动态规划
    43 开辟一个dp数组,dp[i]表示从0-i户可以打劫到的最大钱数。
    44 则有dp[i] = max(dp[i-1],dp[i-2]+nums[i])。
    45 第(i-1)户打劫到的最大钱数+不打劫第i户,
    46 与第(i-2)户打劫的最大钱数+打劫第i户,
    47 两者中的最大值。
    48 还要分情况把nums==None, len(nums)==1, len(nums)==2 分别讨论
    49 """
    50 class Solution(object):
    51     def rob(self, nums):
    52         if nums == []:  #bug if nums == None or len(nums) < 0:
    53             return 0
    54         if len(nums) == 1:                 #len为1的情况
    55             return nums[0]
    56         if len(nums) == 2:                 #len为2的情况
    57             return max(nums[0], nums[1])
    58         dp = [0] * len(nums)               #开辟dp数组,记录最大的钱数
    59         dp[0] = nums[0]                    #将dp[0]初始化
    60         dp[1] = max(nums[0], nums[1])      #将dp[1]初始化
    61         for i in range(2, len(nums)):
    62             dp[i] = max(dp[i-1], nums[i] + dp[i-2]) #!!!动态转移方程
    63         return max(dp)
  • 相关阅读:
    PermissionError: [Errno 1] Operation not permitted: '/tmp/tmpg255ml7f' -> '/tmp/jieba.cache'
    远程连接MySql
    Unity 连接MySql数据库
    Unity WIndows语音识别(一)关键字识别
    Mac M1原生(ARM64)Golang dev&debug
    记一次思考:中级开发的突破之道
    深入web workers (上)
    indexDB出坑指南(二)
    html+css展示空白类字符的技巧
    前端冷知识
  • 原文地址:https://www.cnblogs.com/yawenw/p/12261713.html
Copyright © 2011-2022 走看看