[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 ...
随机推荐
- c++ 类内部函数调用虚函数
做项目的过程中,碰到一个问题. 问题可以抽象为下面的问题: 普通人吃饭拿筷子,小孩吃饭拿勺子. class People { public: void eat() { get_util_to_eat( ...
- 【线程】Thread中的join介绍
因为sleep.wait.join等阻塞,可以使用interrupted exception异常唤醒. 一.作用 Thread类中的join方法的主要作用就是同步,它可以使得线程之间的并行执行变为串行 ...
- Elasticsearch学习之深入聚合分析一---基本概念
首先明白两个核心概念:bucket和metric 1. bucket:一个数据分组 city name 北京 小李 北京 小王 上海 小张 上海 小丽 上海 小陈 基于city划分buckets,划分 ...
- jquery validate使用笔记
1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家. 1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素 3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...
- 2555: SubString[LCT+SAM]
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MB Submit: 2601 Solved: 780 [Submit][Status][ ...
- 【CF700E】Cool Slogans 后缀自动机+线段树合并
[CF700E]Cool Slogans 题意:给你一个字符串S,求一个最长的字符串序列$s_1,s_2,...,s_k$,满足$\forall s_i$是S的子串,且$s_i$在$s_{i-1}$里 ...
- 【CQgame】[幸运方块 v1.1.3] [Lucky_Block v1.1.3]
搬家首发!!! 其实从初一我就写过一些小型战斗的游戏,但是画面都太粗糙,代码也比较乱,也就是和两三个同学瞎玩,但自从观摩了PoPoQQQ大神的游戏,顿时产生了重新写一部游戏的冲动,于是各种上网查找各种 ...
- html处理富文本内容,避免XSS工具类
import org.apache.commons.lang3.StringEscapeUtils;import org.jsoup.Jsoup;import org.jsoup.safety.Whi ...
- java使用AES256解密
网上关于java用AES加密解密的文章有很多,我这里只用到解密(加密是服务器那边做^_^),所以更简洁一些: public class AES256Utils { private static fin ...
- Bagging和Boosting的概念与区别
随机森林属于集成学习(ensemble learning)中的bagging算法,在集成算法中主要分为bagging算法与boosting算法, Bagging算法(套袋发) bagging的算法过程 ...