【LeetCode题解】动态规划:从新手到专家(一)
文章标题借用了Hawstein的译文《动态规划:从新手到专家》。
1. 概述
动态规划( Dynamic Programming, DP)是最优化问题的一种解决方法,本质上状态空间的状态转移。所谓状态转移是指每个阶段的最优状态(对应于子问题的解)可以从之前的某一个或几个阶段的状态中得到,这个性质叫做最优子结构。而不管之前这个状态是如何得到的,这被称之为无后效性。
DP问题中最经典的莫过于01背包问题:
有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。
用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值;则其状态转移方程:
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
“将前i件物品放入容量为v的背包中”这个子问题,若只考虑第i件物品的策略(放或不放),那么就可以转化为一个只牵扯前i-1件物品的问题。如果不放第i件物品,那么问题就转化为“前i-1件物品放入容量为v的背包中”,价值为f[i-1][v];如果放第i件物品,那么问题就转化为“前i-1件物品放入剩下的容量为v-c[i]的背包中”,此时能获得的最大价值就是f[i-1][v-c[i]]再加上通过放入第i件物品获得的价值w[i]。
2. 题解
LeetCode题目 | 归类 |
---|---|
53. Maximum Subarray | 子数组最大和 |
121. Best Time to Buy and Sell Stock | 子数组最大和 |
122. Best Time to Buy and Sell Stock II | 子序列最大和 |
123. Best Time to Buy and Sell Stock III | |
188. Best Time to Buy and Sell Stock IV | |
55. Jump Game | |
70. Climbing Stairs | |
62. Unique Paths | |
63. Unique Paths II | |
64. Minimum Path Sum | 最短路径 |
91. Decode Ways |
以下代码既有Java,也有Go。
53. Maximum Subarray
子数组最大和问题,求解方法可用Kadane算法。
121. Best Time to Buy and Sell Stock
题目大意:给定数组\(a[..]\),求解\(\max a[j] - a[i] \quad j > i\)。
解决思路:将数组a的相邻值相减(右边减左边)变换成数组b,上述问题转变成了求数组b的子数组最大和问题.
// Kadane algorithm to solve Maximum subArray problem
public int maxProfit(int[] prices) {
int maxEndingHere = 0, maxSoFar = 0;
for (int i = 1; i < prices.length; i++) {
maxEndingHere += prices[i] - prices[i - 1];
maxEndingHere = Math.max(maxEndingHere, 0);
maxSoFar = Math.max(maxEndingHere, maxSoFar);
}
return maxSoFar;
}
122. Best Time to Buy and Sell Stock II
之前问题Best Time to Buy and Sell Stock的升级版,对交易次数没有限制,相当于求解相邻相减后形成的子序列最大和——只要为正数,则应计算在子序列内。
public int maxProfit(int[] prices) {
int max = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
max += (prices[i] - prices[i - 1]);
}
}
return max;
}
123. Best Time to Buy and Sell Stock III
最多允许交易两次。
public int maxProfit(int[] prices) {
int sell1 = 0, sell2 = 0;
int buy1 = Integer.MIN_VALUE, buy2 = Integer.MIN_VALUE;
for (int price : prices) {
buy1 = Math.max(buy1, -price); // borrow
sell1 = Math.max(sell1, buy1 + price);
buy2 = Math.max(buy2, sell1 - price);
sell2 = Math.max(sell2, buy2 + price);
}
return sell2;
}
188. Best Time to Buy and Sell Stock IV
最多允许交易k次。当k >= n/2时,在任意时刻都可以进行交易(一次交易包括买、卖),因此该问题退化为了问题122. Best Time to Buy and Sell Stock II。其他情况则有递推式:
\]
其中,\(c_{i,j}\)表示在\(t\)时刻共\(i\)次交易产生的最大收益。
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if (n <= 1) {
return 0;
}
// make transaction at any time
else if (k >= n / 2) {
return maxProfit122(prices);
}
int[][] c = new int[k + 1][n];
for (int i = 1; i <= k; i++) {
int localMax = -prices[0];
for (int j = 1; j < n; j++) {
c[i][j] = Math.max(c[i][j - 1], localMax + prices[j]);
localMax = Math.max(localMax, c[i - 1][j] - prices[j]);
}
}
return c[k][n - 1];
}
public int maxProfit122(int[] prices) {
int max = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
max += (prices[i] - prices[i - 1]);
}
}
return max;
}
55. Jump Game
限制当前最大跳跃数,问是否能到达最后一个index。需要反向往后推演。
public boolean canJump(int[] nums) {
int n = nums.length, index = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (i + nums[i] >= index)
index = i;
}
return index <= 0;
}
70. Climbing Stairs
题目大意:每一次可以加1或加2,那么从0加到n共有几种加法?
假定\(d_i\)表示加到i的种数,那么就有递推式\(d_i = d_{i-1} + d_{i-2}\)。
func climbStairs(n int) int {
if(n < 1) {
return 0;
}
d := make([]int, n+1)
d[1] = 1
if n >= 2 {
d[2] = 2
}
for i := 3; i<=n; i++ {
d[i] = d[i-1] + d[i-2]
}
return d[n]
}
62. Unique Paths
题目大意:求解从左上角到右下角的路径数。
路径数递推式:\(c_{i,j}= c_{i-1,j} + c_{i,j-1}\)。
func uniquePaths(m int, n int) int {
f := make([][]int, m)
for i := range f {
f[i] = make([]int, n)
}
// handle boundary condition: f[][0] and f[0][]
f[0][0] = 1
for i := 1; i < m; i++ {
f[i][0] = 1
}
for j := 1; j < n; j++ {
f[0][j] = 1
}
for i := 1; i < m; i++ {
for j := 1; j < n; j++ {
f[i][j] = f[i][j - 1] + f[i - 1][j]
}
}
return f[m-1][n-1]
}
63. Unique Paths II
加了限制条件,有的点为obstacle——不允许通过。上面的递推式依然成立,只不过要加判断条件。另外,在实现过程中可以用一维数组代替二维数组,比如说按行或按列计算。
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int columnSize = obstacleGrid[0].length;
int[] c = new int[columnSize];
c[0] = 1;
for (int[] row : obstacleGrid) {
for (int j = 0; j < columnSize; j++) {
if (row[j] == 1)
c[j] = 0;
else if (j >= 1)
c[j] += c[j - 1];
}
}
return c[columnSize - 1];
}
64. Minimum Path Sum
题目大意:从矩阵的左上角到右下角的最短路径。
加权路径值\(c_{i,j}= \max (c_{i-1,j},c_{i,j-1}) + w_{i,j}\),其中,\(w_{i,j}\)为图中边的权值。
// the shortest path for complete directed graph
func minPathSum(grid [][]int) int {
var m, n = len(grid), len(grid[0])
f := make([][]int, m)
for i := range f {
f[i] = make([]int, n)
}
// handle boundary condition: f[][0] and f[0][]
f[0][0] = grid[0][0]
for i := 1; i < m; i++ {
f[i][0] = f[i - 1][0] + grid[i][0]
}
for j := 1; j < n; j++ {
f[0][j] = f[0][j-1] + grid[0][j]
}
for i :=1; i < m; i++ {
for j := 1; j<n; j++ {
if(f[i-1][j] < f[i][j-1]) {
f[i][j] = f[i-1][j] + grid[i][j]
} else {
f[i][j] = f[i][j-1] + grid[i][j]
}
}
}
return f[m-1][n-1]
}
91. Decode Ways
求解共有多少种解码情况。
public int numDecodings(String s) {
int n = s.length();
if (n == 0 || (n == 1 && s.charAt(0) == '0'))
return 0;
int[] d = new int[n+1];
d[n] = 1;
d[n - 1] = s.charAt(n - 1) == '0' ? 0 : 1;
for (int i = n-2; i >= 0; i--) {
if(s.charAt(i) == '0')
continue;
else if(Integer.parseInt(s.substring(i, i+2)) <= 26)
d[i] += d[i + 2];
d[i] += d[i + 1];
}
return d[0];
}
【LeetCode题解】动态规划:从新手到专家(一)的更多相关文章
- [LeetCode 题解]:Best Time to Buy and Sell Stock
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Say you ha ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
- Leetcode之动态规划(DP)专题-877. 石子游戏(Stone Game)
Leetcode之动态规划(DP)专题-877. 石子游戏(Stone Game) 亚历克斯和李用几堆石子在做游戏.偶数堆石子排成一行,每堆都有正整数颗石子 piles[i] . 游戏以谁手中的石子最 ...
- LeetCode题解分类汇总(包括剑指Offer和程序员面试金典,持续更新)
LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) 剑指Offer 数据结构 链表 序号 题目 难度 06 从尾到头打印链表 简单 18 删除链表的节点 简单 22 链表中倒数第k个节点 ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
随机推荐
- js中运动框架的封装
//获取非行间样式的封装 function setStyle(obj,name){ // 考虑兼容性问题 if(obj.currentStyle){//不兼容火狐和谷歌 return obj.curr ...
- SHELL命令集锦
1.定时任务crond使用. crontab -e -u www文件编辑保存在/var/spool/cron/www文件中. 参考示例: */1 * * * * /usr/local/php/bin/ ...
- [0] C# 扩展方法(Extension Method)
有时有这样的情况,有一个类,你不能修改它,但你又想对它扩展(添加一个方法),这个时候就可以用到扩展方法了.请看下面的例子: using System;using System.Collections. ...
- ex3多类问题和NN中的前向传播
昨日去了趟无锡,前天下了暴雨,所以昨天给我的感觉天气很好,天蓝云白的,以后在这边学习估计也是一件很爽的事情,且昨日通知书业寄到学校了,附赠了一份研究生数学建模的传单,我搜了搜近几年的题目,感觉统计 ...
- 流畅的python学习笔记:第三章
字典的变种: OrderedDict 首先来看下面的代码,在一个字典中,有name,age,city,在遍历这个字典的时候.顺序却是随机的,不是按照我们添加的顺序也就是name->age-> ...
- 通过PHP前端后台交互/通过ajax前端后台交互/php基础传输数据应用/简单的留言版/简单的注册账户/简单的登录页/
前 言 PHP 通过上一篇博客,注册账号与登录页面--前后台数据交互 跳转转到index主页,接下来进入主页留言板功能,通过ajax向后台传输数据,同时发表留言. 具体的内容分析如下 ...
- 微信 python 接口 -- itchat 文档
itchat 一. 安装 $ pip install itchat 特殊的字典使用方式 通过打印 itchat 的用户以及注册消息的参数, 可以发现这些值都是字典. 但实际上 itchat 精心构造了 ...
- 开源Inno Setup官网下载、安装、打包教程(官网安装向导中文语言包)
安装Inno Setup篇 1.搜索Inno Setup 2.下载Inno Setup 3.选择下载最新 innosetup-5.5.9-unicode.exe 版本(innosetup-5.5.9. ...
- 【Android Developers Training】 7. 添加Action Buttons
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- jQery的链式操作和商城简易导航栏
今天要记录的是jq的一些简单操作.项目的需求是做一个导航栏,单机不同的商品名称链接,显示相应的内容.用js来写的话相对代码量要多一些,选择jqrey一行则可以搞定,下面呢是我的代码和效果图 这个是初始 ...