当前位置:网站首页>Dynamic rules - array compression optimization
Dynamic rules - array compression optimization
2022-07-22 01:41:00 【sword to coding】
First, let's look at a simple and classic dynamic programming problem
link : Minimum path sum
Ideas
Recurrence of violence
General dynamic programming problems , First use violence recursion to sort out ideas , Relatively easy . Then, according to the recursive equation of violent recursion, it is changed into the dynamic programming equation .
class Solution {
public int minPathSum(int[][] grid) {
if(grid==null||grid.length==0){
return 0;
}
return process(0,0,grid);
}
// The meaning of the recursive equation indicates , When it comes to the point (i,j) When in position , Change the shortest path from the point to the point in the lower left corner and
public int process(int i,int j,int[][] grid){
if(i> grid.length-1){
return Integer.MAX_VALUE/2;
}
if(j> grid[0].length-1){
return Integer.MAX_VALUE/2;
}
if(i== grid.length-1&&j==grid[0].length-1){
return grid[i][j];
}
int p1=grid[i][j]+process(i+1,j,grid);
int p2=grid[i][j]+process(i,j+1,grid);
return Math.min(p1,p2);
}
Violence recurs to dynamic programming
Look at the parameters of recursive equations in violent recursion , There are two variable parameters , There is an invariant parameter , Two of these variable parameters can be regarded as dp The table is a two-dimensional array , Then change to dynamic programming according to the dependency
class Solution {
public int minPathSum(int[][] grid) {
if(grid==null||grid.length==0){
return 0;
}
if(grid.length==1&&grid[0].length==1){
return grid[0][0];
}
int lenX=grid.length;
int lenY= grid[0].length;
int[][] dp=new int[lenX][lenY];
dp[lenX-1][lenY-1]=grid[lenX-1][lenY-1];
for(int i=lenX-1;i>=0;i--){
for(int j=lenY-1; j>=0; j--){
dp[i][j]=Math.min(grid[i][j]+get(dp,i+1,j,grid), grid[i][j]+get(dp,i,j+1,grid));
}
}
return dp[0][0];
}
public int get(int[][] dp,int i,int j,int[][] grid){
int lenX=dp.length;
int lenY=dp[0].length;
if(i>lenX-1){
return Integer.MAX_VALUE/2;
}
if(j>lenY-1){
return Integer.MAX_VALUE / 2;
}
if(i==lenX-1&&j==lenY-1){
return grid[i][j];
}
return dp[i][j];
}}
A two-dimensional dp Compress the array to one dimension dp
From the above we can see , The current point depends on the left point and the lower point , This is a , As long as dependencies like this depend on adjacent points , You can use array compression for optimization .
public int minPathSum(int[][] grid) {
if(grid==null||grid.length==0){
return 0;
}
int lenX=grid.length;
int lenY= grid[0].length;
int[] dp=new int[lenY];
dp[lenY-1]=grid[lenX-1][lenY-1];
for(int i=lenY-2;i>=0;i--){
dp[i]=dp[i+1]+grid[lenX-1][i];
}
for(int i=lenX-2;i>=0;i--){
dp[lenY-1]+=grid[i][lenY-1];
for(int j=lenY-2;j>=0;j--){
dp[j]=Math.min(dp[j],dp[j+1])+grid[i][j];
}
}
return dp[0];
}
Convert a two-dimensional array into a one-dimensional array .
边栏推荐
- js正则案例
- 动态规划多重背包一维
- Kaptcha验证码的配置与使用
- mysql8 貌似不支持myisam 分区表,PolarDB-X 支持myisam 的分区表吗?
- Deploy server
- Uniapp, wechat applet input regular verification can only be input as numbers and decimal places
- Pyqt5 packaging error, missing files, such as importerror: opencv loader: missing configuration file: ['config.py'] Check
- “我放弃编程,写了一本130万字的硬科幻小说”
- windows10上安装mysql 5.7.37
- Codeforces Round #805 A-F题解
猜你喜欢
记一次远程Debug调试Minecraft服务器插件经历
Easyexcel is easy to use
C language: leak detection and filling (I)
SpaceX与芭比娃娃制造商美泰达成主题玩具产品协议
重大突破!首款国产科学计算软件研发成功
Visualization: you must know these ten data visualization tool software platforms
Apache Atlas 2.2版本安装
【集训DAY8】Tent【数学】【DP】
联想小新air13 pro重装win10时出现找不到存储设备驱动
weirdo The interview topics include toilet habits, eating time and sleeping time
随机推荐
【集训DAY8】Series【矩阵乘法】
【数模/数学规划模型】
好用的办公网优化工具OneDNS
[dongle vulnerability notification] Apache spark command injection vulnerability scheme
2022-7-17 FTP客户端项目实现 - 总结
【集训DAY9】Light Tank【动态规划】
Spa single page learning and understanding
go gorm mysql报错:Error 1292: Incorrect datetime value: ‘XXX‘ for column ‘created_at‘ at row 1
SpaceX has reached an agreement with Mattel, a Barbie doll manufacturer, on theme toy products
Database transaction isolation level
Learning path PHP -- thinkphp5 + windows server to achieve scheduled tasks
06-1、友元、初始化列表初始化、nei‘bu
Rust learning notes -rust language foundation
Go Gorm MySQL reports an error: error 1292: incorrect datetime value: 'xxx' for column 'created_ at‘ at row 1
SSM integrates other components
js正则案例
SSM整合其他组件
洛谷——P1616 疯狂的采药
如何修改MySQL监听IP地址
Advanced C language: data storage (deep analysis - integer)