你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。

给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/house-robber-ii

列:

输入:nums = [2,3,2]
输出:3
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
class Solution(object):def rob(self, nums):""":type nums: List[int]:rtype: int"""n = len(nums)if n == 0:  # 没有房屋的情况return 0elif n == 1:  # 只有一间房屋的情况return nums[0]elif n == 2:  # 只有两间房屋的情况return max(nums[0], nums[1])  # 返回数额更多的那一间# 因为第一家和最后一家收尾相连,所以分两种情况:抢了第一家就不能抢最后一家# 情况一:不抢第一家dp1 = [0] * n  # 赋予初值以便存储dp1[0], dp1[1] = 0, nums[1]  #第一家不抢,打劫第一家的情况设置为0,打劫第二家的情况直接设置为第二家的现金for i in range(2, n):  # 从第三家开始遍历dp1[i] = max(dp1[i-1], nums[i] + dp1[i-2])  # 比较包含前一家的情况和前两家加当前房屋现金和的情况,取现金数额更高的打劫方法。# 情况二:不抢最后一家,判断与上面类似dp2 = [0] * ndp2[0], dp2[1] = nums[0], max(nums[0], nums[1])for i in range(2, n-1):dp2[i] = max(dp2[i-1], nums[i] + dp2[i-2])return max(dp1[n-1], dp2[n-2])  # 取两种情况中较大的那一种情况

打家劫舍(首尾相连)Python解法相关推荐

  1. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  2. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  3. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  4. LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法

    题目地址:Poor Pigs - LeetCode There are 1000 buckets, one and only one of them is poisonous, while the r ...

  5. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  6. LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法

    题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...

  7. LeetCode 148. Sort List--面试算法题--C++,Python解法

    LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...

  8. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  9. LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法

    题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...

  10. LeetCode 144. Binary Tree Preorder Traversal--二叉树前序遍历--反向压栈--迭代-栈,递归--C++,Python解法

    题目地址:Binary Tree Preorder Traversal - LeetCode Given a binary tree, return the preorder traversal of ...

最新文章

  1. LabVIEW实现CRC校验
  2. “智源论坛Live”报名 | 清华大学游凯超:领域适配前沿研究--场景、方法与模型选择...
  3. Python 一种接触就容易爱上的语言一
  4. python和sqlserver_利用python实现mysql数据库向sqlserver的同步
  5. 大学生英语fif测试系统_英语听力解题技巧
  6. 多机器使用setnx 设置同一个key_Redisson分布式锁的简单使用
  7. vue3修改index.html中的link,icon无效问题,需要配置pwa
  8. 【Kafka】Could not find or load main class kafka.tools.ConsumerOffsetChecker
  9. Python(一)缺点
  10. 网件rax40可以刷梅林_支持WiFi6,带宽达到3000Mbps,网件RAX40路由器评测 | 钛极客...
  11. IO流介绍与File类
  12. 灰色系统理论及其应用 (七) :道路交通事故灰色 Verhulst 预测模型
  13. 论文参考文献格式与设置
  14. #Linux中的GCC编程# 20170731 C培训作业
  15. 终于搞懂python通过twain模块控制扫描仪了
  16. u盘无法读出显示计算机被锁定,U盘无法打开,提示被写保护了怎么办?
  17. 关于邮件签名证书的常见问题
  18. 企业微信公众号怎么建立和运营?
  19. python流水灯简单程序_简单流水灯的实现程序
  20. UML图解和UML代码范例

热门文章

  1. 在家办公是什么“下场”,来看看美国历史
  2. 一个使用 SAP Spartacus 开发的电商网站 - Lego 乐高
  3. TypeScript 书写 .d.ts 文件的一些注意事项
  4. SAP Spartacus activeBaseSite的设置逻辑
  5. 我的工作日报 - 2020-9-15 星期二
  6. SAP Spartacus和product相关的标准normalizer
  7. SpringBoot启动时就会自动去连接mongdo DB指向的url
  8. how does tomcat access js - SAPUI5 Core Libraries
  9. Angular dependency injection - how injection is parsed
  10. How does model reference pass from app view to master view