当前位置:网站首页>[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];
}
}
边栏推荐
- 在OpenCloudOS使用snap安装.NET 6
- 解决表单action属性传参时值为null的问题
- Dotnet uses crossgen2 to readytorun DLL to improve startup performance
- Day 6 script and animation system
- Adapter mode
- sqlcmd 连接数据库报错
- 读取pdf图片并识别内容
- R language plot visualization: plot to visualize overlapping histograms, and use geom at the bottom edge of the histogram_ The rugfunction adds marginal rugplots
- [Unity]EBUSY: resource busy or locked
- 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
猜你喜欢

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

What is the best way to learn machine learning

接口自动化框架脚手架-利用反射机制实现接口统一发起端

【NLP】今年高考英语AI得分134,复旦武大校友这项研究有点意思

Dotnet uses crossgen2 to readytorun DLL to improve startup performance

手把手教你处理 JS 逆向之 SVG 映射

sentinel

丢弃 Tkinter!简单配置快速生成超酷炫 GUI!
![[unity][ecs] learning notes (II)](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[unity][ecs] learning notes (II)

使用 ABAP 操作 Excel 的几种方法
随机推荐
理想中的接口自动化项目
学习机器学习的最佳路径是什么
ffmpeg录音录像
物联网无线通信应用中6种融合定位技术
Decorator
How to use dataant to monitor Apache apisex
[unity][ecs] learning notes (III)
港伦敦金行情走势图所隐藏的信息
mysql打不开,闪退
[unity][ecs] learning notes (II)
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
bye! IE browser, this route edge continues to go on for IE
How to distinguish and define DQL, DML, DDL and DCL in SQL
第六天 脚本与动画系统
[unity][ecs] learning notes (I)
一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!
Resolution: overview of decentralized hosting solution
Application of X6 in data stack index management
一种跳板机的实现思路
Ideal interface automation project






