当前位置:网站首页>Dynamic planning learning notes
Dynamic planning learning notes
2022-07-25 05:40:00 【lsely】
Dynamic programming learning notes
List of articles
Dynamic programming
Fibonacci sequence
Fibonacci sequence Every number All are The sum of the first two digits of this number .
Fibonaccci Sequence
1 1 2 3 5 8 13 21
If we use a formula to represent any number in the sequence, it is (n>1):
f i b ( n ) = f i b ( n − 1 ) + f i b ( n − 2 ) fib(n) = fib(n-1) + fib(n-2) fib(n)=fib(n−1)+fib(n−2)
In code :
long long fib(long long n){ // Find the... In the Fibonacci sequence n Number
if(n==1||n==2){
return n;
}
return fib(n-1)+fib(n-2); // Continue to recursive
}
But when n large , Amount of computation It's just Extremely large , When we decompose each calculation of the computer, we will find many Repeat the calculation , And the larger the calculation , The more calculations are repeated , Then we have to use a method : Dynamic programming .
Dynamic programming
Dynamic programming Is the opposite solution Optimization problems A kind of Design method or thought , It's not an algorithm , This method contains many algorithms , Such as recursive , decompose etc. . Dynamic programming solves more complex problems by decomposing the original problem into several sub problems .
One type, three signs
There are many people who are interested in Dynamic programming such Optimization plan Understand very thoroughly and make a summary , I summarize it as **“ One type, three signs ”**.
" Type I ” It refers to The optimal solution model .
“ Three signs ” refer to Optimal substructure 、 Subsequent invalidity and Memory search .
Optimization plan
First simulate a scenario : The teacher arranged 100 A very complicated problem , But this 100 All the questions are the same , Then we only need to calculate the result of one problem , Just copy it to other questions ( Of course, you can also calculate one by one , But a person who really counts one by one must be a fool —), And unfortunately , The computer is such a fool —, And then we can use it Memory search , Is to record the results of each calculation , If you need a certain calculation result in the subsequent calculation , Call directly .
// Use dynamic programming to solve Fibonacci sequence
long long fib(long long n,long long* memo) {
if (n == 1 || n == 2) return 1;
if (memo[n] != 0) return memo[n];
memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
return memo[n];
}
summary :
The focus of Dynamic Planning :
1. decompose , Decompose the original problem into several sub problems .
2. One type, three signs , Make an optimal solution model for the problem , At a certain stage, once determined , It will not be affected by the later stage and memory search .
... Here is the end , Here is the end ...
边栏推荐
- JWT(json web token)
- 50: Chapter 5: develop admin management service: 3: develop [query whether the admin user name already exists, interface]; (this interface can only be called when logging in; so we have written an int
- Leetcode 15: sum of three numbers
- 求求你别再用 System.currentTimeMillis() 统计代码耗时了,真的太 Low 了!
- Detailed explanation of stepn chain game system development mode (Sports money making mode)
- 50:第五章:开发admin管理服务:3:开发【查询admin用户名是否已存在,接口】;(这个接口需要登录时才能调用;所以我们编写了拦截器,让其拦截请求,判断用户是否是登录状态;)
- Bug --- redis deserialization failed
- Analyzing the principle of DNS resolution in kubernetes cluster
- Linear algebra (3)
- Oracle 用户A删除用户B名下的一张表后,用户B可以通过回收站恢复被删除的表吗?
猜你喜欢

npx和npm区别

LeetCode 15:三数之和

Microservices and related component concepts

New discovery of ROS callback function

Leetcode 202. 快乐数(一点都不快乐)

C编程 --“最大子数组的和” 的动态规划的解法

ThreadLocal

Game 302 of leetcode

50 places are limited to open | with the news of oceanbase's annual press conference coming!

Programming hodgepodge (II)
随机推荐
Introduction summary of using unirx in unity
剖析kubernetes集群内部DNS解析原理
Leetcode 15: sum of three numbers
聊聊 Redis 是如何进行请求处理
Is the Huatai account opened by qiniu safe to use?
sqlilabs less-29
动态规划学习笔记
Equal proportion of R language test group: use the prop.test function to test whether the success proportion of the two groups is equal
Realsense d435i depth map optimization_ High precision mode
Single sign on (one sign on, available everywhere)
接口幂等性
R language uses wilcox.test function to perform Wilcox signed rank test to obtain confidence interval of population median (set conf.level parameter to specify confidence level and size of confidence
微服务 - Hystrix 熔断器
HTB-Optimum
An SQL execution process
Leetcode 204. count prime numbers (wonderful)
Leetcode 0122. the best time to buy and sell stocks II
编程大杂烩(一)
Switch NPM source to private source library
新时代生产力工具——FlowUs 息流全方位评测