Java for LeetCode 085 Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
解题思路:
求01矩阵中,全是1的子矩阵的最大面积。
把矩阵按照每一行看做是直方图,可以转化为上一题,JAVA实现如下:
static public int maximalRectangle(char[][] matrix) {
if(matrix.length==0||matrix[0].length==0)
return 0;
int result=0;
int[] dp=new int[matrix[0].length];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++)
if(matrix[i][j]=='1')
dp[j]++;
else dp[j]=0;
result=Math.max(result, largestRectangleArea(dp));
}
return result;
}
public static int largestRectangleArea(int[] height) {
Stack<Integer> stk = new Stack<Integer>();
int ret = 0;
for (int i = 0; i <= height.length; i++) {
int h=0;
if(i<height.length)
h=height[i];
if (stk.isEmpty() || h >= height[stk.peek()])
stk.push(i);
else {
int top = stk.pop();
ret = Math.max(ret, height[top] * (stk.empty() ? i : i - stk.peek() - 1));
i--;
}
}
return ret;
}
Java for LeetCode 085 Maximal Rectangle的更多相关文章
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【LeetCode】085. Maximal Rectangle
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...
- Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 085 Maximal Rectangle 最大矩形
给定一个填充了 0 和 1 的二进制矩阵,找到最大的只包含 1 的矩形并返回其面积.例如,给出以下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 6 详见:http ...
- Java for LeetCode 221 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- 【leetcode】Maximal Rectangle (hard)★
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
随机推荐
- 用PHP实现弹出消息提示框
方法一: echo "<script>alert('提示内容')</script>"; 方法二: echo '<script language=&qu ...
- 解决 ultraedit 菜单字体模糊
新装完ultraedit后,使用时菜单里的字体非常小,模糊不清非常不爽. 1.安装好UltraEdit后,到安装目录里,复制一份Uedit32.exe文件用于修改.2.使用UltraEdit打开复制的 ...
- Django 创建APP - 简单路由系统案例
架构图: setting.py: INSTALLED_APPS = [ ... 'bootstrap', ] myapp -> myapp -> urls.py from django.c ...
- 区间最小值(2) (线段树 更新区间)2015年 JXNU_ACS 算法组暑假第一次周赛
区间最小值(2) Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Subm ...
- linux中shell script的追踪与调试
Shell调试篇 sh [-nvx] scripts.sh -n:不要执行script,仅查询语法的问题: -v:在执行script前,先将script的内容输出到屏幕上: -x:将使用到的scrip ...
- vue2.0 仿手机新闻站(一)项目开发流程
vue仿手机新闻站: 1.拿到静态页面,直接用vue边布局,边写 2.假数据 没有用任何UI组件,切图完成 做项目基本流程: 1.规划组件结构 Nav.vue Header.vue Home.vue ...
- OPENCV中数字图像处理知识运用
cvZero():是让矩阵的值都为0,有初始化的作用,或者说清零~比如说:IplImage img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);%创建一 ...
- 简单记录一次ORA-00600: internal error code, arguments: [2662]
接上一个,REDO报错搞定后OPEN数据库时又报错ORA-00600: internal error code, arguments: [2662]. 原因是_ALLOW_RESETLOGS_CORR ...
- C# 调用API接口处理公共类 自带JSON实体互转类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- 云数据库 RDS 版怎么创建数据库和账号MySQL 5.7版
若要使用云数据库RDS,您需要在实例中创建数据库和账号.对于MySQL 5.7版本的实例,您需要通过RDS控制台创建一个初始账号,然后可以通过数据管理(DMS)控制台创建和管理数据库.本文将主要介绍在 ...