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的更多相关文章

  1. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  2. 198. House Robber(动态规划)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  3. 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:二叉树下的不能相邻,求能 ...

  4. [LeetCode] 198. House Robber 打家劫舍

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

  5. Leetcode 198 House Robber

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

  6. Java for LeetCode 198 House Robber

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

  7. (easy)LeetCode 198.House Robber

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

  8. 【LeetCode】198 - House Robber

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

  9. Java [Leetcode 198]House Robber

    题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

随机推荐

  1. 步进电机加减速S曲线算法

    一.Sigmoid 函数 1.1 Sigmoid函数原型 1.2 sigmoid函数波形: 由图形可看出在-10时已经接近于0,一般取值区间在[-5,5]. 1.3 sigmoid函数的导数 转载CS ...

  2. 吴裕雄--天生自然MySQL学习笔记:MySQL 序列使用

    MySQL 序列是一组整数:1, 2, 3, ...,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现. 使用 AUTO_INCREMENT ...

  3. 吴裕雄--天生自然 PHP开发学习:数组排序

    <?php $cars=array("Volvo","BMW","Toyota"); sort($cars); print_r($ca ...

  4. 吴裕雄--天生自然 JAVASCRIPT开发学习:测试 jQuery

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Opencv从文件中播放视频

    1.VideoCapture()括号中写视频文件的名字,在播放每一帧的时候,使用cv2.waitKey()设置适当的持续时间,太低会播放的很快,太高会很慢,通常情况下25毫秒就行了. 2.获取相机/视 ...

  6. ubuntu16.04 pcl安装教程

    https://blog.csdn.net/zkj126521/article/details/80157351 https://blog.csdn.net/e_small/article/detai ...

  7. 数据处理pandas

    1.缺失值时间戳不为NaN,为NaT, 同样判断都为isna()或notna()方法2.删值\去重 df.dropna() df.drop_duplicates() 3.上下值插值 df.fillna ...

  8. PHP的isset()函数 一般用来检测变量是否设置

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  9. 吴裕雄--天生自然ShellX学习笔记:Shell 文件包含

    和其他语言一样,Shell 也可以包含外部脚本.这样可以很方便的封装一些公用的代码作为一个独立的文件. Shell 文件包含的语法格式如下: . filename # 注意点号(.)和文件名中间有一空 ...

  10. delphixe7支持MYSQL连接的方式

    由于工作需要,给出配套能用的版本,目前仅在win10 64位 XE7测试通过,如果换成其他环境,请根据自己的环境使用如下路径的dbxmys.dll 32位系统 E:\Program Files (x8 ...