当前位置:网站首页>Tag dynamic programming - preliminary knowledge for question brushing -1 Dynamic programming five part problem solving method + lt.509 Fibonacci number / Sword finger offer 10 I + lt.70. Climbing stai
Tag dynamic programming - preliminary knowledge for question brushing -1 Dynamic programming five part problem solving method + lt.509 Fibonacci number / Sword finger offer 10 I + lt.70. Climbing stai
2022-06-22 02:59:00 【Caicai's big data development path】
Initial understanding of dynamic programming
Dynamic programming : DP, If a problem has many overlapping subproblems , Using dynamic programming is the most effective , Therefore, every state in dynamic programming must be derived from the previous state , This is different from greed ,
- Greed has no state , But choose the best one directly from the local
As we all know
The dynamic gauge is made of ** The previous state ** Derived from , Greed is the local direct selection of the optimal, It is enough to brush the questions .
Problem solving steps of dynamic programming
- State transition formula ( The recursive formula ) Very important , But the dynamic gauge is not just a recursive formula !
Five steps of dynamic programming
- determine dp Array (dp table) And subscripts The meaning of ;
- determine The recursive formula
- dp How to initialize an array
- determine traversal order
- Give an example to deduce dp Array
DP What's the problem Debug

How to calculate the time complexity of recursive algorithm ? It is to multiply the number of subproblems by the time needed to solve a subproblem .
lt.509. Fibonacci number
Case study

Train of thought analysis 1 , Recurrence of violence
The state transition equation has been given , We can get the answer directly by recursion .

Train of thought analysis II , Remove double counting , Use an array to record the calculated value
from f(n) = f(n - 1) + f(n - 2); It can be seen that f(n) Only the first two values are required for each calculation , We record the first two values in an array ;
- determine dp Meaning of array and subscript
- Determine the recurrence formula
- dp Initialization of an array
- Determine the traversal order , Sequential traversal
- Give an example to deduce dp Array
class Solution {
public int fib(int n) {
// Two , Use an array to record each f(n - 1) and f(n - 2);
//1. determine dp Meaning of array and subscript , requirement f(n), There has to be dp(n), So the array length is n+1;
int[] dp = new int[n + 1];
//2. Determine the recurrence formula , Has been given ; f(n) = f(n - 1) + f(n - 2);
//3. dp Initialization of an array , Has been given
if(n == 0 || n == 1)return n;
dp[0] = 0;
dp[1] = 1;
//4. Determine the traversal order , Sequential traversal ,
for(int i = 2; i <= n; i++){
dp[i] = dp[i- 1] + dp[i- 2];
}
return dp[n];
}
}
Train of thought Analysis III , Optimize the space complexity
from f(n) = f(n - 1) + f(n - 2); It can be seen that f(n) Only the first two values are required for each calculation , We maintain two variables directly , Keep updating ;
class Solution {
public int fib(int n) {
// 3、 ... and ,
if(n == 0 || n == 1)return n;
int dp2 = 0;
int dp1 = 1;
int dp = 0;
for(int i = 2; i <= n; i++){
dp = dp1 + dp2;
dp2 = dp1; // Note that there , The order must not be disordered ! The first dp[n-1] The value is assigned to dp[n-2];
dp1 = dp;// And then dp[n] The value is assigned to dp[n - 1];
}
return dp;
}
}
The finger of the sword Offer 10- I. Fibonacci sequence
Case study

Thought analysis : Strike while the iron is hot , Use the third method above directly
This question is based on the above one , It is required to take the mold during the calculation
class Solution {
public int fib(int n) {
if(n == 0 || n == 1)return n;
int dp2 = 0;
int dp1 = 1;
int dp = 0;
for(int i = 2; i <= n; i++){
dp = (dp1 + dp2) % 1000000007;
dp2 = dp1;
dp1 = dp;
}
return dp;
}
}

lt.70. climb stairs
Case study

Train of thought analysis 1 , DP Five steps
determine DP Array and subscript meaning
dp[n] = x, n It means how many steps there are in the stairs , x It's on the stairs n The existing method of climbing stairs
Determine the recurrence formula ( State transition formula )
- The title gives , Can only climb at a time 1 Or climb 2 A stair , So we are climbing n A step , Only by 1 A step or 2 From the state transition of three steps .
- So the state transition formula is :
dp[n] = dp[n - 1] + dp[n-2];in other words , n-1 Possible ways to climb stairs ( There's still 1 A stair ) + n-2 Possible ways to climb stairs ( There's still 2 A stair )
dp How to initialize an array
- Can only climb at a time 1 A or 2 A stair , therefore dp[1] = 1, dp[2] = 2;
Determine the traversal order , From the perspective of recurrence formula , Sequential traversal .
(debug) Give an example to deduce dp Array .

class Solution {
public int climbStairs(int n) {
//1. determine dp Array , He climbed i(n) A stair , Yes dp[i] Medium method
int[] dp = new int[n + 1];
//2. Determine the recurrence formula
//dp[i] = dp[i - 1] + dp[i - 2];
//3. dp How to initialize an array
dp[1] = 1;
dp[2] = 2;
//4. traversal order
// Reverse traversal , know n-1, n-2 A step method , Introduction n A step jump
int sum = 0;
for(int i = 2; i <= n; i++){
dp[i] = dp[i - 1] + dp[i - 2];
sum += dp[i];
}
return sum;
}
}
The two most common questions about climbing stairs

1. Why? f(n) = f(n - 1) + f(n - 2) ? How to think of ?
First , The title gives , Can only climb at a time 1 A stair , perhaps 2 A stair , Before we climb n A step , Only by climbing up n-1 Behind the steps , Climb again 1 A stair , Or climb n-2 Behind the steps , Climb again 2 A stair
Some people may have such doubts , Why?
(n-1)The climbing method of steps and(n-2)The climbing method of steps , No repetition ?- First climb n-1 And climb n-2 The number of steps is different , How can I repeat ? These are two complete cases ! We climb n-1 Climb the stairs 1 Steps have arrived n steps , We climb n In the case of steps , Climb again 2 A step has been reached n steps .
Then some people may have the following questions , Why can't we finish climbing n-2 After mediation , Climb again 1 Steps and 1 A stair , Or climb again 2 Two steps ?
- –> You are climbing n-2 Behind the steps , One more step , It doesn't mean you've climbed ahead n-1 There are various ways to climb the steps .
2. Why climb n-1 In the case of step , There is still one order left , Or climb n-2 There are two steps left , Why not add 1 or 2 Well ?
Actually, I am climbing n-1 In this case , We have various combinations of climbing methods , But the distance climbs n rank , We can only climb again 1 A stair , This 1 Namely n-1 After a step climb , Climb again n The only case of a step ;
Empathy , We are finishing the climb n-2 steps , Got it n-2 After all kinds of climbing methods of steps , I can only climb again 2 Steps to get n A stair .
Just to give you an example ,
The interview questions about climbing stairs (dbc)
1. Expand 1

2. Expand 2

3. Expand 3

4. Expand 4

5. Expand 5

边栏推荐
猜你喜欢

【Percona-Toolkit】系列之pt-table-checksum和pt-table-sync 数据校验修复神器
![Comprehensive interpretation by enterprise reviewers: enterprise growth of [State Grid] China Power Finance Co., Ltd](/img/4e/345ceb4e91aae844d7ab53ad3fd988.png)
Comprehensive interpretation by enterprise reviewers: enterprise growth of [State Grid] China Power Finance Co., Ltd

Implementation principle and application practice of Flink CDC mongodb connector

The brand, products and services are working together. What will Dongfeng Nissan do next?

Library management system (PHP final report)

【爬虫笔记2】鼠标事件与截图方法、常用攻击方法

Right and left vertical time axis with serial number
![[9. submatrix sum]](/img/97/32f11e2f26a1f313c808fcc1cd27b3.png)
[9. submatrix sum]

JVM makes wheels

ATM机模拟系统
随机推荐
mocklog_ Simulation log
最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA
[1. quick sort]
An article thoroughly learns to draw data flow diagrams
Force buckle 142 Circular linked list II
Force buckle 461 Hamming distance
Live broadcast on June 22 | zhanzhihui, South China Institute of Technology: evolutionary computing for expensive optimization
C++ primer Chapter 2 summary of variables and basic types
Brief analysis of application source code of neo4j intelligent supply chain
关于mysql的int型主键自增问题
Force buckle 102 Sequence traversal of binary tree
Parallel search DSU
Day14QProgressBar2021-10-17
图像元数据(Metadata)获取与修改
【爬虫笔记1】环境搭建和必要工具Selenium
6、 MySQL data definition language (1)
ATM机模拟系统
【5. 高精度减法】
Graphacademy course explanation: Fundamentals of neo4j graph data science
Force buckle 295 Median data flow
