当前位置:网站首页>Lecture on linear dynamic programming
Lecture on linear dynamic programming
2022-06-10 13:14:00 【Conan 2】
Special lecture on linear dynamic programming
Linear programming concept
The main feature of dynamic programming is that the derivation of state is based on the problem scale i i i From small to large ( Or push it all the way from the back , such as 「 Stone game 」、「2140. Solve intellectual problems 」 It is pushed from the back to the front ), The solution of the larger scale problem depends on the solution of the smaller scale problem .
Generally, the steps to solve such problems are as follows :
- Sure d p [ i ] dp[i] dp[i] The meaning of representation / attribute
- According to the meaning of the title , Find the corresponding State transition equation
- Determine the initial value
- If it's from front to back , Then you need to make sure d p [ 0 ] dp[0] dp[0] perhaps d p [ 1 ] dp[1] dp[1]
- If it is from the back to the front , Then you need to make sure d p [ n ] dp[n] dp[n] perhaps d p [ n − 1 ] dp[n - 1] dp[n−1]
LeetCode 877. Stone game
class Solution {
public boolean stoneGame(int[] piles) {
int n = piles.length;
long[][] dp = new long[n + 1][n + 1];
// dp[i][j] Representative slave interval [i,j] The maximum score obtained between
// dp[i][j] = Math.max(piles[i] + dp[i + 1][j], dp[i][j - 1] + plies[j])
// dp[i][i] = 0
// dp[0][n - 1] > 0 ? true : false;
for (int i = 0 ; i < n; i++) {
dp[i][i] = 0;
}
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
dp[i][j] = Math.max(piles[i] + dp[i + 1][j], dp[i][j - 1] + piles[j]);
}
}
return dp[0][n - 1] > 0 ? true : false;
}
}
LeetCode 2140. Solve intellectual problems
class Solution {
public long mostPoints(int[][] q) {
int n = q.length;
// dp[i] The meaning of is from the subscript i~n-1 The maximum score obtained
// dp[i] = Math.max(dp[Math.min(n, q[i][1])] + q[i][0], dp[i])
long[] dp = new long[n + 1];
for (int i = n - 1; i >= 0; i--) {
dp[i] = Math.max(dp[Math.min(n, i + q[i][1] + 1)] + q[i][0], dp[i + 1]);
}
return dp[0];
}
}
LeetCode 72. Edit distance
class Solution {
public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
// meaning :dp[i][j] representative Word1 From the subscript 0~i and word2 From the subscript 0~j The minimum number of operations used
// State transition equation :dp[i][j] = Math.min(dp[i -1][j] + 1,Math.min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + (word1.charAt(i - 1) != word2.charAt(j - 1) ? 1 : 0)));
int[][] dp = new int[n + 1][m + 1];
if (n * m == 0) {
return n + m;
}
for (int i = 0; i <= n; i++) {
dp[i][0] = i;
}
for (int j = 0 ; j <= m ; j++) {
dp[0][j] = j;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int left = dp[i -1][j] + 1;
int down = dp[i][j - 1] + 1;
int left_down = dp[i - 1][j - 1];
if (word1.charAt(i - 1) != word2.charAt(j - 1)) {
left_down++;
}
dp[i][j] = Math.min(left_down, Math.min(left, down));
}
}
return dp[n][m];
}
}
边栏推荐
- 'getcolor (int) 'is deprecated, getcolor is obsolete
- If the files and graphics are lost, it means that you don't need the office developed by yourself
- Introduction to assembly language - Summary
- SAP Field Service Management 和微信集成的案例分享和实现介绍
- 从解读 BDC 自动生成的代码谈起,讲解 SAPGUI 的程序组成部分试读版
- Don't mistake "it informatization" for "super project"
- 不吐不快
- The deep neural network classifies nearly 2billion images per second, and the new brain like optical classifier chip is on nature
- Idea custom configuration link Nacos
- MySQL数据库(26):视图 view
猜你喜欢

2022 ciscn preliminary PWN complete WP

如何才能把团队给带解散。。。

Don't mistake "it informatization" for "super project"

Today, a couple won the largest e-commerce IPO in Hong Kong

Cvpr2022|aconvnetforthe2020s & how to design neural network Summary

手机厂商“返祖”,只有苹果说不

On distributed transaction

TIDB 初级课程体验 8 (集群的管理维护, 添加一个TIKV节点)

Baidu programmers were sentenced to nine months for deleting the database. The one click unbinding function of the mobile phone number was released. Twitter compromised with musk again. Today, more bi

Introduction of Altium Designer
随机推荐
Automatic Mapping of Tailored Landmark Representations for Automated Driving and Map Learning 论文阅读
汇编语言入门-总结
In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May
How about the one-stop machine learning opening platform mlflow?
Error:top-left corner pixel must be either opaque white or transparent.
[mobile robot] principle of wheel odometer
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
Development trend of Web Development
[NLP] NLP full path learning recommendation
mTabLayout. setOnTabSelectedListener is deprecated
MYSQL 主库操作大表DDL ,从库崩溃与系统参数错误设置
拷贝和删除文件
從解讀 BDC 自動生成的代碼談起,講解 SAPGUI 的程序組成部分
SparkStreaming实时数仓 问题&回答
SAP Field Service Management 和微信集成的案例分享和实现介绍
Leetcode 96. 不同的二叉搜索樹
'setbackgrounddrawable() 'is deprecated, setbackgrounddrawable is obsolete
Baidu programmers were sentenced to nine months for deleting the database. The one click unbinding function of the mobile phone number was released. Twitter compromised with musk again. Today, more bi
NanoMQ Newsletter 2022-05|v0.8.0 发布,新增 WebHook 拓展接口和连接认证 API
Ant financial services Yang Jun: evolution of ant data analysis platform and application of data analysis methods