maximal-rectangle Java code
来源:互联网 发布:初中英语词汇软件 编辑:程序博客网 时间:2024/05/21 11:19
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area.
import java.io.*;import java.util.*;public class Solution { public int maximalRectangle(char[][] matrix) { if(matrix==null || matrix.length==0 || matrix[0].length==0) return 0; int max = 0; int rows = matrix.length; int cols = matrix[0].length; int[] height = new int[cols]; int[] left = new int[cols]; //左边界(与高度相关进行判断) int[] right = new int[cols]; //右边界 for(int i=0; i<cols; i++){ right[i] = cols; } for(int i=0; i<rows; i++){ int curLeft = 0; int curRight = cols; //当前左边界和右边界 //更新左边界 for(int j=0; j<cols; j++){ if(matrix[i][j] == '1'){ height[j]++; left[j] = Math.max(curLeft, left[j]); }else{ height[j] = 0; curLeft = j+1; left[j] = 0; } } for(int j=cols-1; j>=0; j--){ if(matrix[i][j]=='1'){ right[j] = Math.min(curRight, right[j]); //很重要的一步 }else{ right[j] = cols; curRight = j; } //max = Math.max(max, (right[j]-left[j])*height[j]); } for(int k=0;k<cols;k++){ if(max<(right[k]-left[k])*height[k]){ max=(right[k]-left[k])*height[k]; } } } return max; }}
阅读全文
0 0
- maximal-rectangle Java code
- CODE 48: Maximal Rectangle
- [Leetcode] Maximal Rectangle (Java)
- Maximal Rectangle (Java)
- [LeetCode][Java] Maximal Rectangle
- Maximal Rectangle leetcode java
- leetcode 日经贴,Cpp code -Maximal Rectangle
- LeetCode85 Maximal Rectangle java题解
- [leetcode]85. Maximal Rectangle(Java)
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maven搭建SSM框架
- 斧钺智能推出两款无人机,二次发力专业级市场
- 6.1(2)
- CC2530中的MAC层确认帧(兼容IEEE 802.15.4-2006)
- 线性链表
- maximal-rectangle Java code
- 6.1(1)
- 全卷积神经网络 图像语义分割实验:FCN数据集制作,网络模型定义,网络训练(提供数据集和模型文件,以供参考)
- Linux之CentOS-7安装步骤总结
- wxWidget教程(9)——网络编程HTTP与SOCKET
- java–流已经启动或关闭
- linux各个版本发行图
- opencv(c++)图像操作
- 栅栏