当前位置:网站首页>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
边栏推荐
- @Valid parameter verification does not take effect
- Docker安装Oracle_11g
- How to compress video size while adding watermark with one click?
- [eight sorts ②] select sort (select sort, heap sort)
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- 【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
- What is commercial endowment insurance? Is commercial endowment insurance safe?
- Creation of volume group for AIX storage management (I)
- ACM tutorial - quick sort (regular + tail recursion + random benchmark)
- Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master
猜你喜欢

Xinniuniu blind box wechat applet source code_ Support flow realization, with complete material pictures

RFID让固定资产盘点更快更准

Edge extraction edges based on Halcon learning_ image. Hdev routine

excel数据透视表

Excel search and reference function

Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model

Principle of finding combinatorial number and template code

Minimize the error

【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面

【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
随机推荐
Cat Party (Easy Edition)
Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
Collection: comprehensive summary of storage knowledge
只是以消费互联网的方式和方法来落地和实践产业互联网,并不能够带来长久的发展
Datawhale 社区黑板报(第1期)
Sql--- related transactions
[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
@Valid parameter verification does not take effect
教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
Some understandings of graph convolution neural network r-gcn considering relations and some explanations of DGL official code
Docker安装Oracle_11g
什么是商业养老保险?商业养老保险安全靠谱吗?
AIX存储管理之卷组属性的查看和修改(二)
"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
Global and Chinese market of avionics systems 2022-2028: Research Report on technology, participants, trends, market size and share
If the browser is accidentally closed, how does react cache the forms filled out by users?
学习笔记3--高精度地图关键技术(上)
About asp Net core uses a small detail of datetime date type parameter
How to open an account for individual stock speculation? Is it safe?