[LeetCode] 198. House Robber _Easy tag: Dynamic Programming
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 = ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 计算完成率 SQL
计算完成率 SQL ,), ,) ) AS XX_完成率
- JUnit —— TestSuite 的使用
首先说一下,suite ,中文是 一套,一组 的意思. 那么,TestSuite,顾名思义,就是用来运行一组测试的. 如何使用呢 ? 假设有个这样的测试类 StudentDAOTest ,代码如下: ...
- Android studio 安装已经下载好的gradle.zip文件【ubuntu 14.04 LTS环境】
一 下载 gradle-3.3-all.zip 包 http://download.csdn.net/detail/t6546545/9732412 http://www.fxxz.com/soft/ ...
- linux的shell后门尝试以及Cython转成C代码编译
零.背景 最近研究了一下之前的反弹shell的python代码块,写了一点代码尝试在LInux下绑定和反弹shell(正反向),看了一些代码,基本是两种思路.1.本地shell的输入输出通过管道与so ...
- 关于linux例行任务crontab的使用
Linux 例行性任务(也叫周期性任务)命令使用:crontab1.crontab -l 查看当前用户的任务2.crontab -e 编辑(设置)当前用户的任务,执行行不用重启crond服务.3 ...
- 【CF316G3】Good Substrings 后缀自动机
[CF316G3]Good Substrings 题意:给出n个限制(p,l,r),我们称一个字符串满足一个限制当且仅当这个字符串在p中的出现次数在[l,r]之间.现在想问你S的所有本质不同的子串中, ...
- 【C#】简单计算器源代码
form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...
- JIRA - 使用指南(项目跟踪管理工具)
第一章.前言 JIRA 是澳大利亚 Atlassian 公司开发的一款优秀的问题跟踪管理软件工具,可以对各种类型的问题进行跟踪管理,包括缺陷.任务.需求.改进等.JIRA采用J2EE技术,能够跨 ...
- Web前端编码规范[转]
先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...
- 7.19python昨日复习和多线程
关于GIL锁的经典面试题!!