日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Leetcode--85. 最大矩形

發布時間:2024/7/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode--85. 最大矩形 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

給定一個僅包含?0 和 1 的二維二進制矩陣,找出只包含 1 的最大矩形,并返回其面積。

示例:

輸入:
[
? ["1","0","1","0","0"],
? ["1","0","1","1","1"],
? ["1","1","1","1","1"],
? ["1","0","0","1","0"]
]
輸出:6

思路:動態規劃

用dp二維數組保存每個位置以它結尾的,1的長度,例如dp[0][2]位置,1的長度為1,dp[2][4]位置,1的長度為5

知道各個位置以他結尾的1的長度之后,開始考慮矩形的寬度

以本題的結果為例,dp[2][4]長度為5,現在以他結尾的矩形面積max暫定為5,之后向上搜索,dp[1][4]長度為3,但是我們的高度增加了,所以現在面積是2*3=6,增大了,更新max為6即可。

提交的代碼:

class Solution {
? ? public int maximalRectangle(char[][] matrix) {
? ? ? ? ?? ?int n = matrix.length;
? ? ? ? int m=0;
? ? ? ? if(n>0)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //題中有測試案例輸入為空,因此直接寫int?m = matrix[0].length;會報錯
? ? ? ? {
? ? ? ? ? ? ?m = matrix[0].length;
? ? ? ? }
? ? ? ? if(m==0&&n==0)
? ? ? ? {
? ? ? ? ? ? return 0;
? ? ? ? }
?? ??? ?int i,j,max=-1,t=0,k;
?? ??? ?int[][] dp1 = new int[n][m];
?? ??? ?for(i=0;i<n;i++)
?? ??? ?{
?? ??? ??? ?for(j=0;j<m;j++)
?? ??? ??? ?{
?? ??? ??? ??? ?if(matrix[0][0]=='1')
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?dp1[0][0]=1;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if(matrix[0][0]=='0')
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?dp1[0][0]=0;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if(i==0&&j>0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if(matrix[i][j]=='1')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j] = dp1[i][j-1]+1;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j] = 0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if(j==0&&i>0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if(matrix[i][j]=='1')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j]=1;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if(i>0&&j>0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if(matrix[i][j]=='1')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j] = dp1[i][j-1]+1;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j] = 0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?t = dp1[i][j];
?? ??? ??? ??? ?for(k=i;k>=0;k--)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?t = java.lang.Math.min(dp1[k][j], t);? ?//比較目前j列已經遍歷過的行中,找到最小長度進行計算
?? ??? ??? ??? ??? ?if(t*(i-k+1)>max)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?max = t*(i-k+1);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return max;
? ? }
}

完整代碼:


public class Solution85 {
?? ?public static int maximalRectangle(char[][] matrix) {
?? ??? ?int n = matrix.length;
? ? ? ? int m=0;
? ? ? ? if(n>0)
? ? ? ? {
? ? ? ? ? ? ?m = matrix[0].length;
? ? ? ? }
? ? ? ? if(m==0&&n==0)
? ? ? ? {
? ? ? ? ? ? return 0;
? ? ? ? }
?? ??? ?int i,j,max=-1,t=0,k;
?? ??? ?int[][] dp1 = new int[n][m];
?? ??? ?for(i=0;i<n;i++)
?? ??? ?{
?? ??? ??? ?for(j=0;j<m;j++)
?? ??? ??? ?{
?? ??? ??? ??? ?if(matrix[0][0]=='1')
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?dp1[0][0]=1;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if(matrix[0][0]=='0')
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?dp1[0][0]=0;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if(i==0&&j>0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if(matrix[i][j]=='1')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j] = dp1[i][j-1]+1;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j] = 0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if(j==0&&i>0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if(matrix[i][j]=='1')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j]=1;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if(i>0&&j>0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if(matrix[i][j]=='1')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j] = dp1[i][j-1]+1;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?dp1[i][j] = 0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?t = dp1[i][j];
?? ??? ??? ??? ?for(k=i;k>=0;k--)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?t = java.lang.Math.min(dp1[k][j], t);
?? ??? ??? ??? ??? ?if(t*(i-k+1)>max)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?max = t*(i-k+1);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return max;
? ? }
?? ?public static void main(String[] args)
?? ?{
?? ??? ?char[][] a = {{'1','0','1','0','0'},{'1','0','1','1','1'},{'1','1','1','1','1'},{'1','0','0','1','0'}};
?? ??? ?System.out.println(maximalRectangle(a));
?? ?}
}
?

總結

以上是生活随笔為你收集整理的Leetcode--85. 最大矩形的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。