[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 ...
随机推荐
- spring cache 详解
Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我 ...
- Python pyQt4/pyQt5 学习笔记1(空白窗口,按钮,控件事件,控件提示,窗体显示到屏幕中间,messagebox)
PyQt4是用来编写有图形界面程序(GUI applications)的一个工具包.PyQt4作为一个Python模块来使用,它有440个类和超过6000种函数和方法.同时它也是一个可以在几乎所有主流 ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
- php-config
php-config php-config 是一个简单的命令行脚本用于获取所安装的 PHP 配置的信息. 在编译扩展时,如果安装有多个 PHP 版本,可以在配置时用 --with-php-config ...
- VMware虚拟机安装Ubuntu系统英文改中文的方法
首先点击右上角的这个桌面 1,Change Desktop Background 图片发自简书App 2.到系统设置(System Settings)--- 点击Language Support ...
- yii---生产链接的方法
yii生成链接的方法: Yii::$app->urlManager->createUrl('xxx/xxx') <?= Yii::$app->urlManager->cr ...
- JavaEE JSP 学习笔记
一.JSP简介 1.也是SUN公司推出的开发动态web资源的技术,属于JavaEE技术之一.由于原理上是Servlet, 所以JSP/Servlet在一起. 二.HTML.Servlet和JSP 1. ...
- java8新特性之Optional类
NullPointException可以说是所有java程序员都遇到过的一个异常,虽然java从设计之初就力图让程序员脱离指针的苦海,但是指针确实是实际存在的,而java设计者也只能是让指针在java ...
- POJ3268 Silver Cow Party【最短路】
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...
- 深圳MPD大会,五大专题一会尽享
深圳MPD大会,五大专题一会尽享 2013年9月,深圳的高温将慢慢褪去,炎炎夏日也会变得稍微清凉一些.但9月It届的峰会活动却没有丝毫的锐减.9月7-8日深圳将迎来MPD大会2013的收官之站. MP ...