当前位置:网站首页>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];
}
}
边栏推荐
- Wei Lai: "pinches" the data and "pinches" the future
- MySQL数据库(26):视图 view
- 'getcolor (int) 'is deprecated, getcolor is obsolete
- From "chemist" to developer, from Oracle to tdengine, two important choices in my life
- Software project management 6.10 Cost budget
- Site investigation system
- #yyds干货盘点# 解决剑指offer:跳台阶扩展问题
- Program, calculate 2/1+3/2+5/3+8/5 Value of. It is required to calculate the sum of the first n items and keep 2 decimal places (starting from the second item of the sequence, the numerator of each it
- Use soapUI tool to generate SMS interface code
- Code free may event Microsoft low code matrix update; Multiple industry reports released
猜你喜欢

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

MySQL 服务演进

今天,一对情侣拿下香港最大电商IPO
![Vdo-slam source code reading notes [2] local optimization and global optimization](/img/01/7ce7113737d9799ac2684788d9f08d.jpg)
Vdo-slam source code reading notes [2] local optimization and global optimization
![[mobile robot] principle of wheel odometer](/img/91/fbe7ad8e37a70384f043a785d34865.png)
[mobile robot] principle of wheel odometer

Automatic Mapping of Tailored Landmark Representations for Automated Driving and Map Learning 论文阅读

QT database application 22 file coding format recognition

NanoMQ Newsletter 2022-05|v0.8.0 发布,新增 WebHook 拓展接口和连接认证 API

Program, calculate 2/1+3/2+5/3+8/5 Value of. It is required to calculate the sum of the first n items and keep 2 decimal places (starting from the second item of the sequence, the numerator of each it

Leetcode 96. 不同的二叉搜索树
随机推荐
The APK file does not exist on disk
13、 System call and shell (freesanding shell, terminal and job control)
Start with interpreting the code automatically generated by BDC, and explain the trial version of the program components of sapgui
手机厂商“返祖”,只有苹果说不
Timeline and logistics information. You don't need stepview at all
12、 Process address space (PMAP; vdso; MMAP)
[summary] individual competition supplement POJ - 3041 asteroids & codeforces - 173b chamber of Secrets
2022 ciscn preliminary PWN complete WP
VDO-SLAM: A Visual Dynamic Object-aware SLAM System 论文阅读
IQR箱线图
编写程序,计算2/1+3/2+5/3+8/5.....的值。要求计算前n项之和,保留2位小数(该序列从第二项起,每一项的分子是前一项分子与分母的和,分母是前一项的分子)
Shi Yigong and other teams posted on the cover of Science: AI and freeze electron microscope revealed the structure of "atomic level" NPC, a breakthrough in life science
Program, calculate 2/1+3/2+5/3+8/5 Value of. It is required to calculate the sum of the first n items and keep 2 decimal places (starting from the second item of the sequence, the numerator of each it
Can qiniu open an account? Is it safe to open an account in qiniu
Automatic mapping of tailored landmark representations for automated driving and map learning
Sohu employees encounter wage subsidy fraud. What is the difference between black property and gray property and how to trace the source?
Performance test plan (plan) template
【抬杠C#】如何实现接口的base调用
Dynaslam ii: carefully coupled multi object tracking and slam
MySQL service evolution