[LC] 198. 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.
Example 1:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12. Solution 1:
Time: O(N)
Space: O(N)
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
if len(nums) <= 2:
return max(nums) max_lst = [0] * len(nums)
max_lst[0] = nums[0]
max_lst[1] = max(nums[0], nums[1])
for i in range(2, len(nums)):
max_lst[i] = max(max_lst[i - 1], max_lst[i - 2] + nums[i])
return max_lst[-1]
Solution 2:
Time: O(N)
Space: O(1)
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
prev_prev, prev, cur = 0, 0, 0
for num in nums:
prev_prev = prev
prev = cur
cur = max(num + prev_prev, prev)
return cur
[LC] 198. House Robber的更多相关文章
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 198. House Robber(动态规划)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- [LeetCode] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Leetcode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- (easy)LeetCode 198.House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 【LeetCode】198 - House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java [Leetcode 198]House Robber
题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
随机推荐
- node,npm,webpack,vue-cli模块化编程安装流程
首先什么都不要管,先装环境. pip是万能的!!! 安装node: pip3 install node 安装npm: pip3 install npm 安装webpack: npm install ...
- Java 静态static关键字,main函数,对象的初始化过程,对象调用成员,单例模式的设计,静态代码块(6)
Java 静态static关键字,静态代码块详情参考:static的使用原理讲解http://www.cnblogs.com/itcqx/p/5519464.html main函数: java Mai ...
- Split string every nth character?
https://stackoverflow.com/questions/9475241/split-string-every-nth-character >>> line = '12 ...
- ruoyi LogUtils
package com.ruoyi.framework.util; import java.io.PrintWriter; import java.io.StringWriter; import ja ...
- 提高WiFi上网速度
https://jingyan.baidu.com/article/1876c852aa668c890b1376c4.html http://www.coozhi.com/youxishuma/you ...
- shift+回车,换行。断点。
在Idea中,shift+回车可以在一行的任意一地方换行. 断点的小知识. debug启动程序后左下角会出现断点的功能选项. 一个竖列 一个横行,没有请求时是灰的. 这里主要讲竖列. 这个是沉默全部断 ...
- Python—冒泡排序算法
冒泡排序 一,介绍 冒泡排序(Bubble Sort)也是一种简单直观的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再 ...
- 爬虫—GEETEST滑动验证码识别
一.准备工作 本次使用Selenium,浏览器为Chrome,并配置好ChromDriver 二.分析 1.模拟点击验证按钮:可以直接使用Selenium完成. 2.识别滑块的缺口位置:先观察图 ...
- vim编辑模式下黑色背景,下来过程中出现白条的问题
问题描述,原本是黑色背景,但是下拉过程中没有文字的地方会变成白色,非常影响美观,搞了好久现在中与改好了.问题如图: 打开-/.vimrc 文件,在如图所示位置加上62-64行代码即可.root用户的添 ...
- Python笔记_第四篇_高阶编程_GUI编程之Tkinter_5.鼠标事件
1. 鼠标点击事件: 图示: 实例: import tkinter from tkinter import ttk # 创建主窗口__编程头部 win = tkinter.Tk() # 设置标题 wi ...