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. 【大数据系列】hadoop集群设置官方文档翻译

    Hadoop Cluster Setup Purpose Prerequisites Installation Configuring Hadoop in Non-Secure Mode Config ...

  2. 2015.7.8js-05(简单日历)

    今天做一个简单的小日历,12个月份,鼠标移动其中一个月份时添加高亮并显示本月的活动.其实同理与选项卡致.不过是内容存在js里 window.onload = function(){ var oMain ...

  3. 使用spring提供的ReflectionUtils简化项目中反射代码的复杂性

    在项目中有时候我们会使用到反射的功能,如果使用最原始的方法来开发反射的功能的话肯能会比较复杂,需要处理一大堆异常以及访问权限等问题.spring中提供了ReflectionUtils 这个反射的工具类 ...

  4. wps插件开发中com组件权限

    需要对wps写一个小的插件,也就是几行代码的事情,但却碰到了一个坑 wps中的com组件的调用和MSoffice非常的相似,几乎只需要把包的头修改一下就可以用了. 比如开发wps文档的插件,需要引用 ...

  5. 【POJ2888】Magic Bracelet Burnside引理+欧拉函数+矩阵乘法

    [POJ2888]Magic Bracelet 题意:一个长度为n的项链,有m种颜色的珠子,有k个限制(a,b)表示颜色为a的珠子和颜色为b的珠子不能相邻,求用m种珠子能串成的项链有多少种.如果一个项 ...

  6. python-django开发学习笔记一

    1.简述 1.1 开发环境 该笔记所基于的开发环境为:windows8.python2.7.5.psycopg2-2.4.2.django1.5.4.pyCharm-2.7.3.以上所描述的软件.插件 ...

  7. Java除法和js

    java 除 向下取整 js 保留小数

  8. POJ-1952 BUY LOW, BUY LOWER(线性DP)

    BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9244 Accepted: 3226 De ...

  9. TOP100summit:【分享实录】Twitter 新一代实时计算平台Heron

    本篇文章内容来自2016年TOP100summit Twitter technical lead for Heron Maosong Fu 的案例分享. 编辑:Cynthia Maosong Fu:T ...

  10. easyui-layout个人实例

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...