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. 这个题目的思路就是用DP(Dynamic programming), 就是我们要找到A[i] 跟它前面几个元素的关系, 这个题目我用三种方法, 第一种是用pre, sec, 第二种是用O(n) Space, 第三种用template的 滚动数组来解决类似的DP题目, 很牛逼. 1. Constraints
1) can be empty
2)element is non-negative
3) edge case, when length < 3 2. ideas DP T: O(n) S: O(1) optimal 1) edge case: l < 3: max([0] + nums)
2) A[i] = max(A[i-2] + nums[i], A[i-1]) 利用滚动数组的时候, 因为当前状态跟之前两个状态有关, 所以模3, % 3 即可将S: O(n) decrease into O(1) 3. Codes 1) use pre, sec
 class Solution:
def houseRopper(self, nums):
l = len(nums)
if l < 3: return max([0] + nums)
pre, sec = nums[0], max(nums[:2])
for i in range(2, l):
ans = max(pre + nums[i], sec)
pre, sec = sec, ans
return sec

2) Template of DP      T: O(n)    S: O(n)

 class Solution:
def houseRopper(self, nums):
n = len(nums)
if n < 3: return max([0] + nums)
f = [0]*n f[0], f[1] = nums[0], max(nums[:2])
for i in range(2, n):
f[i] = max(f[i-2] + nums[i], f[i-1])
return f[n - 1]

3) DP  滚动数组      T: O(n)    S: O(1)

 class Solution:
def houseRopper(self, nums):
n = len(nums)
if n < 3: return max([0] + nums)
f = [0]*3
f[0], f[1] = nums[0], max(nums[:2])
for i in range(2, n):
f[i % 3] = max(f[(i - 2)% 3] + nums[i], f[(i - 1) % 3])
return f[(n - 1) % 3]

4. Test cases

1)  edge cases

2)

[2,7,9,3,1]
												

[LeetCode] 198. House Robber _Easy tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  2. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  3. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  4. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  6. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  7. [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

随机推荐

  1. Microsoft Security Essentials

    https://support.microsoft.com/zh-cn/help/18900/consumer-antivirus-software-providers-for-windows   适 ...

  2. SSL是什么?如何使用?

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TLS与 ...

  3. angularjs笔记《二》

    小颖最近不知怎么了,老是犯困,也许是清明节出去玩,到现在还没缓过来吧,玩回来真的怕坐车了,报了个两日游得团,光坐车了,把人坐的难受得,去了也就是爬山,回来感觉都快瘫了,小颖去的时候还把我家仔仔抱着一起 ...

  4. Linux操作系统定时任务系统 Cron 入门

    cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: /sbin/service c ...

  5. Python拷贝文件脚本

    author : headsen chen date : 2018-12-06  17:56:58 copy_file.py #!/usr/bin/env python from sys import ...

  6. Css中!important的用法

    !important为开发者提供了一个增加样式权重的方法.应当注意的是!important是对整条样式的声明,包括这个样式的属性和属性值 <!DOCTYPE HTML> <html& ...

  7. 在 NHibernate 中一切必须是 Virtual 的吗?

    原文地址:Must Everything Be Virtual With NHibernate? 老赵在博文中 我对NHibernate的感受(2):何必到处都virtual 提到这篇文章,顺便翻译一 ...

  8. python selenium中等待元素出现及等待元素消失操作

    在自动化测试中,很多时候都会有等待页面某个元素出现后能进行下一步操作,或者列表中显示加载,直到加载完成后才进行下一步操作,但时间都不确定,如下图所示 幸运的是,在selenium 2后有一个模块exp ...

  9. “找女神要QQ号码”——java篇

    题目就是这样的: 给了一串数字(不是QQ号码),根据下面规则可以找出QQ号码: 首先删除第一个数,紧接着将第二个数放到这串数字的末尾,再将第三个数删除,并将第四个数放到这串数字的末尾...... 如此 ...

  10. redis -clock_gettime问题

    /home/wm/redis-/deps/jemalloc/src/nstime.c:: undefined reference to `clock_gettime' 这个错误 解决思路如下 .查找实 ...