当前位置:网站首页>Leetcode dynamic planning day 16
Leetcode dynamic planning day 16
2022-07-06 04:49:00 【worldinme】
Realize the idea :
Is very normal , It's no different from yesterday's topic .
Implementation code :
class Solution {
public int minPathSum(int[][] grid) {
int m=grid.length,n=grid[0].length;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i==0&&j!=0){
grid[0][j]=grid[0][j-1]+grid[0][j];
}else if(j==0&&i!=0){
grid[i][0]=grid[i-1][0]+grid[i][0];
}else if(i!=0&&j!=0){
grid[i][j]=Math.min(grid[i-1][j],grid[i][j-1])+grid[i][j];
}
}
}
return grid[m-1][n-1];
}
}
Realize the idea :
For this question , The explanation of the official explanation is very clear :
After defining the state transition equation , The code of this problem is easy to write .
Implementation code :
class Solution {
public int maximalSquare(char[][] matrix) {
int m=matrix.length,n=matrix[0].length;
int[][] dp=new int[m][n];
int maxSum=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(matrix[i][j]=='0'){
dp[i][j]=0;
}else if(i==0||j==0){
dp[i][j]=1;
}else{
dp[i][j]=Math.min(Math.min(dp[i-1][j-1],dp[i-1][j]),dp[i][j-1])+1;
}
maxSum=Math.max(dp[i][j],maxSum);
}
}
return maxSum*maxSum;
}
}
边栏推荐
猜你喜欢
随机推荐
2021robocom robot developer competition (Preliminary)
Postman assertion
集合详解之 Collection + 面试题
Coreldraw2022 new version new function introduction cdr2022
Introduction of several RS485 isolated communication schemes
Platformio create libopencm3 + FreeRTOS project
Selection sort
麦斯克电子IPO被终止:曾拟募资8亿 河南资产是股东
程序员在互联网行业的地位 | 每日趣闻
RTP GB28181 文件测试工具
Visio draws Tai Chi
English Vocabulary - life scene memory method
[Chongqing Guangdong education] engineering fluid mechanics reference materials of southwestjiaotonguniversity
行业专网对比公网,优势在哪儿?能满足什么特定要求?
项目经理,你会画原型嘛?项目经理需要做产品设计了?
Biscuits (examination version)
Luogu deep foundation part 1 Introduction to language Chapter 2 sequential structure programming
Database - MySQL storage engine (deadlock)
Postman前置脚本-全局变量和环境变量
Flody的应用