当前位置:网站首页>[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)
[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)
2022-06-28 10:24:00 【-Blue.】
If it helps you
Praise the blogger
Praise is the biggest encouragement to bloggers
Love launches ~
Catalog
Code Capriccio knowledge planet
Basic knowledge of

- Dynamic programming is derived from the previous state
- The greedy algorithm chooses the best locally and directly
The problem solving steps
- determine dp Array (dp table) And the meaning of subscripts
- Determine the recurrence formula
- dp How to initialize an array
- Determine the traversal order
- Give an example to deduce dp Array
The problem solving steps - concise
- determine dp Array subscript meaning
- The recursive formula
- initialization
- traversal order
- The derivation results
509. Fibonacci number


Dynamic programming
class Solution {
public int fib(int n) {
/* Dynamic programming : 1. determine dp Array (dp table) And the meaning of subscripts - The first i Fibonacci Numbers Value 2. Determine the recurrence formula - F(n) = F(n - 1) + F(n - 2) 3. dp How to initialize an array - F(0) = 0,F(1) = 1 4. Determine the traversal order - From before to after 5. Give an example to deduce dp Array - 0 1 1 2 3 5 8 13 21 34 55 */
if(n < 2) return n;
int a=0, b=1, c=0;
for(int i=1; i<n; ++i){
c = a + b;
a = b;
b = c;
}
return c;
}
}
70. climb stairs
Dynamic programming
class Solution {
public int climbStairs(int n) {
/* Dynamic programming : 1. determine dp Array (dp table) And the meaning of subscripts - Climb the first n A stair , Yes dp[n] Methods 2. Determine the recurrence formula - dp[n] = dp[n-1] + dp[n-2] 3. dp How to initialize an array - dp[1]=1 dp[2]=2 4. Determine the traversal order - From front to back 5. Give an example to deduce dp Array - 1、2、3、5、8 */
if(n<=2) return n;
int a=1, b=2,c=0;
for(int i=2; i<n; ++i){
c = a + b;
a = b;
b = c;
}
return c;
}
}
746. Use the minimum cost to climb the stairs
Dynamic programming
class Solution {
public int minCostClimbingStairs(int[] cost) {
/* 1. determine dp Array (dp table) And the meaning of subscripts - To the first i The minimum physical strength required for a step 2. Determine the recurrence formula - dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]; 3. dp How to initialize an array - dp[0] = cost[0], dp[1] = cost[1] 4. Determine the traversal order - From front to back 5. Give an example to deduce dp Array */
if(cost == null || cost.length == 0) return 0;
if(cost.length == 1) return cost[0];
int[] dp = new int[cost.length];
dp[0] = cost[0];
dp[1] = cost[1];
for(int i=2; i<cost.length; ++i){
dp[i] = Math.min(dp[i-1], dp[i-2]) + cost[i];
}
return Math.min(dp[cost.length-1], dp[cost.length -2]);
}
}
62. Different paths
Dynamic programming
class Solution {
public int uniquePaths(int m, int n) {
/* 1. determine dp Array subscript meaning —— dp[i][j] Possible path types to each coordinate 2. The recursive formula —— dp[i][j] = dp[i-1][j]+ dp[i][j-1] 3. initialization —— dp[i][0]=1 dp[0][i]=1 Initialization can be done horizontally or vertically 4. traversal order —— Line by line 5. The derivation results */
int[][] dp = new int[m][n];
for(int i=0; i<m; ++i){
dp[i][0] = 1;
}
for(int i=0; i<n; ++i){
dp[0][i] = 1;
}
for(int i=1; i<m; ++i){
for(int j=1; j<n; ++j){
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
}
63. Different paths II



Dynamic programming
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
/* 1. determine dp Array subscript meaning —— To dp[i][j] There are several paths 2. The recursive formula —— dp[i][j] = dp[i][j-1] + dp[i-1][j] 3. initialization —— dp[][] Loop initialization , Encounter obstacles , Jump out of , All behind 0 4. traversal order —— From before to after , One layer at a time 5. The derivation results */
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for(int i=0; i<m && obstacleGrid[i][0]==0; ++i){
dp[i][0] = 1;
}
for(int i=0; i<n && obstacleGrid[0][i]==0; ++i){
dp[0][i] = 1;
}
for(int i=1; i<m; ++i){
for(int j=1; j<n; ++j){
if(obstacleGrid[i][j]==1){
continue;
}
dp[i][j] = dp[i][j-1] + dp[i-1][j];
}
}
return dp[m-1][n-1];
}
}
343. integer partition

Dynamic programming
class Solution {
public int integerBreak(int n) {
/* 1. determine dp Array subscript meaning —— Numbers i The maximum product obtained dp[i] 2. The recursive formula —— dp[i] = max({dp[i], (i - j) * j, dp[i - j] * j}); 3. initialization —— dp[2]=1 , dp[3]=2 4. traversal order —— From before to after outside i from 3 Start . From 1 5. The derivation results */
int[] dp = new int[n+1];
dp[2] = 1;
for(int i=3; i<=n; ++i){
for(int j=1; j<=i-j; ++j){
dp[i] = Math.max(dp[i], Math.max(j*(i-j), j*dp[i-j]));
}
}
return dp[n];
}
}
96. Different binary search trees
Carter LAN number
class Solution {
public int numTrees(int n) {
// Carter LAN number
int[] dp = new int[n+1];
dp[0] = 1;
dp[1] = 1;
for(int i=2; i<=n; ++i){
for(int j=1; j<=i; ++j){
dp[i] += dp[j-1] * dp[i -j];
}
}
return dp[n];
}
}
边栏推荐
- SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
- Instant messaging and BS architecture simulation of TCP practical cases
- Matplotlib attribute and annotation
- Bridge mode
- Day 6 script and animation system
- Must the MySQL table have a primary key for incremental snapshots?
- 手把手教你处理 JS 逆向之 SVG 映射
- Sqlcmd database connection error
- Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
- [NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting
猜你喜欢

解析:去中心化托管解决方案概述

Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)

TCP实战案例之即时通信、BS架构模拟

Fastposter v2.8.4 release e-commerce poster generator

无线通信模块定点传输-点对多点的具体传输应用

MySQL cannot be opened. Flash back

Training and recognition of handwritten digits through the lenet-5 network built by pytorch

增强 Jupyter Notebook 的功能,这里有四个妙招

How to distinguish and define DQL, DML, DDL and DCL in SQL

【OpenCV 例程200篇】213. 绘制圆形
随机推荐
Is it safe to open an account with the QR code of CICC securities? Tell me what you know
请教下, 我在本地idea运行flinkcdc的mysql到mysql全量同步,这个是在我本地ide
Huawei OSPF single region
一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
建立自己的网站(11)
fastposter v2.8.4 发布 电商海报生成器
QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])
How to distinguish and define DQL, DML, DDL and DCL in SQL
Google开源依赖注入框架-Guice指南
物联网5种无线传输协议特点大汇总
解决表单action属性传参时值为null的问题
Decorator
再见!IE浏览器,这条路由Edge替IE继续走下去
解析:去中心化托管解决方案概述
接口自动化框架脚手架-参数化工具的实现
R language plot visualization: plot to visualize overlapping histograms, and use geom at the bottom edge of the histogram_ The rugfunction adds marginal rugplots
第六天 脚本与动画系统
【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
Dotnet uses crossgen2 to readytorun DLL to improve startup performance






