当前位置:网站首页>Since I understand the idea of dynamic planning, I have opened the door to a new world
Since I understand the idea of dynamic planning, I have opened the door to a new world
2022-07-02 01:11:00 【Vivien_ oO0】
List of articles
Preface
Today I learned the dynamic programming algorithm , Sorted out some big guy's notes . I decided to write a blog , For students who want to learn dynamic programming algorithm , Let me show you what dynamic planning is .
brief introduction
Dynamic programming (Dynamic Programming)( hereinafter referred to as DP) The core idea of the algorithm is : Divide big problems into small ones to solve , So as to obtain the optimal solution step by step . Solve complex problems by decomposing the original problem into relatively simple problems .
Dynamic programming is not a specific algorithm , It's an idea : To solve a given problem , We need to solve its subproblem , Then the solution of the original problem is obtained according to the sub problem .
Some people may say that this is similar to divide and conquer , The difference is that the solution of divide and conquer algorithm does not depend on the subproblem ,DP It depends on the solution of the subproblem .
Optimal substructure
What is stipulated is the relationship between the original problem and the sub problem
DP What we need to solve is the optimal solution of some problems , That is to find the best one from many solutions to the problem . When we solve the optimal solution of a problem , We divide it into sub problems , Transform bits to solve the optimal solution of the subproblem , Finally, the final result is obtained by combining the optimal solution of the subproblem . In other words, the optimal solution of a problem is determined by the optimal solution of its subproblems .
Repeat the question
What is stipulated is the relationship between sub problems
When we recursively find the optimal solution of each subproblem , There may be some Repeated sub problems , If it's just ordinary recursion , There may be many repeated calculations , and DP It can ensure that each overlapping subproblem will be solved only once . When there are many repeated questions ,DP It can reduce a lot of repeated calculations .
for example : Fibonacci problem : seek f(5)
f(5) / \ f(4) f(3) / \ / \ f(3) f(2)f(2) f(1) / \ f(2) f(1)
f(3) It's double counting , about DP For example, we can build tables to store the answers to sub questions , To avoid double counting
Optimal substructure and repeated subproblem
- There is a choice in the scheme of proving the problem , Leave one or more sub questions after selection
- Recursive description of design subproblems
- It is proved that the optimal solution of the original problem includes the optimal solution of all subproblems
- Prove that the subproblems overlap ( It's not necessary , But if there is no overlap, the efficiency is the same as that of ordinary recursion )
Example : Deepen the understanding
Force to buckle 300 Question longest increasing subsequence
Give you an array of integers nums, Find the length of the longest strictly increasing subsequence .
Subsequence Is a sequence derived from an array , Delete ( Or do not delete ) Elements in an array without changing the order of the rest . for example ,[3,6,2,7] It's an array [0,3,1,6,2,2,7] The subsequence .
for example :
Ideas :
Reduce one at a time : remember f(n)f(n) In order to nn The longest subsequence at the end of the number , Reduce one at a time , Divide the original problem into f(n-1)f(n−1), f(n-2)f(n−2), …, f(1)f(1), common n - 1n−1 Subproblem .n - 1 = 7n−1=7 The sub questions and answers are as follows :
[10, 9, 2, 5, 3, 7, 101] -> [2, 5, 7, 101]
[10, 9, 2, 5, 3, 7] -> [2, 5, 7]
[10, 9, 2, 5, 3] -> [2, 3]
[10, 9, 2, 5] -> [2, 5]
[10, 9, 2] -> [2]
[10, 9] -> [9]
[10] -> [10]
There has been a 7 After the optimal solution of the subproblem , We can find a combination method to get the optimal solution of the original problem :f(6)f(6) Result [2,5,7], 7 < 187<18, At the same time, the length is also f(1)~f(7) in , Ending less than 18 The longest result .f(7) Although the length is 4 Than f(6) Long , But the ending is no less than 18 Of , Can't be combined into the solution of the original problem . The final result is the number of the longest sequence .
The above combination can be written into a formula , The state transition equation :
f(n)=maxf(i)+1 among i<n And a[i]<a[n]
Dynamic programming solution
[10, 9, 2, 5, 3, 7, 101] -> [2, 5, 7, 101]
[10, 9, 2, 5, 3, 7] -> [2, 5, 7]
[10, 9, 2, 5, 3] -> [2, 3]
[10, 9, 2, 5] -> [2, 5]
[10, 9, 2] -> [2]
[10, 9] -> [9]
[10] -> [10]
From here we can see that -> How to get the sequence on the right ?
We look from the bottom up
- The first is 10, So the length of the longest sequence is recorded as 1
- The second is 9, It has only 10 And 9 < 10 , So the length of the current subsequence is still 1
- The third number is 2, Similarly, the previous numbers are larger than it, so the current subsequence length is still 1
- The fourth number is 5, We have to 2 Smaller than his , And the length of the subsequence of two is 1, add 5 So the length becomes 2
- The fifth number is 3, We have to 2 Smaller than his , And the length of the subsequence of two is 1, add 3 So the length becomes 2
- The sixth number is 7, Obviously, the front 2,5,3 All ratio 7 Small , So we add the longest sequence smaller than it ,3 perhaps 5, So to 7, His sequence length becomes 3.
- The seventh digit 101, Similarly, the longest sequence smaller than him in front is 3, Plus its own length is 4
- Last digit 18,, The longest sequence smaller than him in front is numbers 7 The length of one line of is 3, So its subsequence is 4
- The final longest subsequence is the longest of all subsequences . by 4
public static int lengthOfLIS(int[] nums) {
if (nums.length==0){
return 0;
}
// Define an equal length array To store the sequence length
int[] arr = new int[nums.length];
arr[0] = 1;
// The shortest sequence length is 1
int ans = 1;
// Used to calculate the length of subsequence
for (int i = 1;i<nums.length;i++){
// The shortest is oneself
arr[i] = 1;
// Look for the front than i Small number
for (int j =0;j<i;j++){
// If you find a value smaller than the current value be +1 Is that when i The longest sequence before
if (nums[j]<nums[i]){
// Compare all the smaller ones Find the longest
arr[i] = Math.max(arr[j]+1,arr[i]);
}
}
// When to exit for loop Description has found the containing nuns[i] The longest sequence of to update ans
ans = Math.max(ans,arr[i]);
}
// Return the result
return ans;
}
DP It can be gradually promoted by filling in the form , So as to obtain the optimal solution
For recursive solution , I will not , Ha ha ha ha
边栏推荐
- Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
- [WesternCTF2018]shrine writeup
- Review notes of compilation principles
- Excel PivotTable
- Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
- Basis of deep learning neural network
- 笔者更加愿意将产业互联网看成是一个比消费互联网要丰富得多的概念
- King combat power query renamed toolbox applet source code - with traffic main incentive advertisement
- Docker安装Oracle_11g
- 【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
猜你喜欢

Minimize the error

Appium inspector can directly locate the WebView page. Does anyone know the principle

Schrodinger's Japanese learning applet source code

Edge extraction edges based on Halcon learning_ image. Hdev routine

2022 safety officer-a certificate examination questions and online simulation examination

Iclr2022 | spherenet and g-spherenet: autoregressive flow model for 3D molecular graph representation and molecular geometry generation

Principle of finding combinatorial number and template code

The 8-year salary change of testers makes netizens envy it: you pay me one year's salary per month

2022 operation of simulated examination platform for melting welding and thermal cutting work license

Source code of Qiwei automatic card issuing system
随机推荐
excel数据透视表
Leetcode 45 Jumping game II (2022.02.14)
【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码
2022 high altitude installation, maintenance and removal of test question simulation test platform operation
Global and Chinese market of picture archiving and communication system (PACS) 2022-2028: Research Report on technology, participants, trends, market size and share
Cat Party (Easy Edition)
【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器
Cookie, session, tooken
sso单点登录的实现。
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)
[JS download files through url]
DTL dephossite | prediction method of dephosphorylation sites based on Transfer Learning
站在新的角度来看待产业互联网,并且去寻求产业互联网的正确方式和方法
Synthetic watermelon game wechat applet source code / wechat game applet source code
I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
Recently, three articles in the nature sub Journal of protein and its omics knowledge map have solved the core problems of biology
How to reflect and solve the problem of bird flight? Why are planes afraid of birds?
Excel PivotTable
New version of free mobile phone, PC, tablet, notebook four terminal Website thumbnail display diagram online one click to generate website source code