当前位置:网站首页>First encounter with dynamic programming
First encounter with dynamic programming
2022-06-27 12:23:00 【zhen12321】
- You are the guiding algorithm of how to go in every step of my life
- Mathematically, I call you an ugly mathematical induction , namely k<n-1&&k=n-1 To k<=n The process of .
- State shift , Repeat the question , The optimal substructure is your core .
- You always rely on violent algorithms first .
- It makes ordinary people feel like listing a formula , How to invert parameters step by step , Then the method of finding the optimal solution in this binary result set .
- Memos are your most common weapon , After using it , You are a different person , Time complexity can reach O(n), It's a tree , However, the complexity is so low .
- Many people misunderstand you , Think you are recursive , Recursion, however, is a dead end traversal , Your state transition needs a little semantic comfort .
- You are from the bottom up . From top to bottom tells us to traverse violently from the top . Bottom up is a result of design , And I got an equation that shifts this result , Gradually transfer from the top to the bottom .
Classical problems , climb stairs
Suppose you're climbing the stairs . need n You can reach the top of the building .
Every time you climb 1 or 2 A stair . How many different ways can you climb to the top of the building ?
/** * @param {number} n * @return {number} */
var climbStairs = function(n) {
// clear dp Array , So-called dp, Is the abbreviation of dynamic programming .
var dp=[]
// clear dp Bounds of an array , That is the starting point of our deduction .
dp[1]=1;
dp[2]=2;
// Specially defined variable names , be called tz, The meaning of transfer . there tz The pointer represents 『 state 』 The transfer of .
var tz = 3;// from 3 At first, it can only climb 1 Step , or 2 Step . The third step is to 「 Calculation 」 了 .
// I want to climb n rank , I just want to move to n That's all right. .
while(tz<=n){
dp[tz]=dp[tz-1]+dp[tz-2]; //【 State transition equation 】
tz++;
}
return dp[n]; // Return the result I want
};
边栏推荐
猜你喜欢
随机推荐
Hands on API development
Self taught ADT and OOP
application. Configuration information of properties
log4j的详情配置
Usage of rxjs mergemap
How to modify a node_ Files in modules
This privatized deployed enterprise knowledge base makes telecommuting a zero distance
Jwas: a Bayesian based GWAS and GS software developed by Julia
Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022
alibaba jarslink
Basic usage and principle of fork/join framework
Topic37——64. Minimum path sum
application.properties 的配置信息
解开C语言的秘密《关键字》(第六期)
Interview shock 60: what will cause MySQL index invalidation?
alibaba jarslink
threejs的环境光+点光源+平行光源+球面光 以及hepler理解+阴影()
剑指 Offer 04. 二维数组中的查找
最短编辑距离(线性dp写法)
行业洞察 | 新零售业态下,品牌电商应如何重塑增长?









