2018-09-15 10:23:44

一、Largest Rectangle in Histogram

在求解最大的矩形面积之前,我们先讨论一条最大直方图面积的问题。

问题描述:

问题求解:

解法一、朴素解法,O(n ^ 2)。

解决的思路就是遍历一遍,如果当前的数比后一个数要小,那么当前的额数字肯定不可能是最大面积的右边界,遍历下一个数;

如果当前数比后一个大,那么假设当前的为右边界,向左进行遍历,计算面积最大值。

    public int largestRectangleArea(int[] heights) {
if (heights.length == 0) return 0;
int res = 0;
for (int i = 0; i < heights.length; i++) {
if (i == heights.length - 1 || heights[i] > heights[i + 1]) {
int minHeight = heights[i];
for (int j = i; j >= 0; j--) {
minHeight = Math.min(heights[j], minHeight);
res = Math.max(res, minHeight * (i - j + 1));
}
}
}
return res;
}

解法二、使用堆栈,时间复杂度O(n)。

如何更快的解决这个问题呢?这里需要从另一个角度来考虑这个问题,其实解法一也是一种类似DP的解法,它的核心思路就是固定最后一个数,来获得以当前数为结尾的最大矩形面积。其实还有另一个角度来思考,就是以每个数作为高度能获得的最大面积是多少?其实这个问题就是需要找当前数左右第一个比其低的数,然后就可以得出以当前数字为高度的最大矩形面积,最后我们只需要遍历比较一遍就可以得到最大的结果。

    public int largestRectangleArea(int[] nums) {
int n = nums.length;
int[] l = new int[n];
int[] r = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < nums.length; i++) {
while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) stack.pop();
l[i] = stack.isEmpty() ? 0 : stack.peek() + 1;
stack.push(i);
}
stack.clear();
for (int i = nums.length - 1; i >= 0; i--) {
while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) stack.pop();
r[i] = stack.isEmpty() ? nums.length - 1 : stack.peek() - 1;
stack.push(i);
}
int res = 0;
for (int i = 0; i < n; i++) {
res = Math.max(res, nums[i] * (r[i] - l[i] + 1));
}
return res;
}

二、Maximal Rectangle

问题描述:

问题求解:

有个上一个问题的铺垫,这个问题就很好解决了,针对每一行,可以先求出其高度,然后再对每一行求最大最方图的面积,取max即可。

使用一个height的二维数组进行高度的保存,可以将时间复杂度降到O(mn)

    public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) return 0;
int m = matrix.length;
int n = matrix[0].length;
int[][] height = new int[m][n];
for (int i = 0; i < n; i++) if (matrix[0][i] == '1') height[0][i] = 1;
for (int i = 1; i < m; i++) {
for (int j = 0;j < n; j++) {
if (matrix[i][j] == '0') height[i][j] = 0;
else height[i][j] = 1 + height[i - 1][j];
}
}
int res = 0;
for (int i = 0; i < m; i++) {
res = Math.max(res, helper(height[i]));
}
return res;
} private int helper(int[] nums) {
int n = nums.length;
int[] l = new int[n];
int[] r = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < nums.length; i++) {
while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) stack.pop();
l[i] = stack.isEmpty() ? 0 : stack.peek() + 1;
stack.push(i);
}
stack.clear();
for (int i = nums.length - 1; i >= 0; i--) {
while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) stack.pop();
r[i] = stack.isEmpty() ? nums.length - 1 : stack.peek() - 1;
stack.push(i);
}
int res = 0;
for (int i = 0; i < n; i++) {
res = Math.max(res, nums[i] * (r[i] - l[i] + 1));
}
return res;
}

最大的矩形面积 Maximal Rectangle的更多相关文章

  1. LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)

    84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...

  2. [Swift]LeetCode850. 矩形面积 II | Rectangle Area II

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

  3. LeetCode 223. 矩形面积(Rectangle Area)

    223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...

  4. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  5. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. [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 ...

  7. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  8. Largest Rectangle in a Histogram(最大矩形面积,动态规划思想)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. [Swift]LeetCode223. 矩形面积 | Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

随机推荐

  1. 思维导图软件 xMind 基本用法

    官方下载:https://www.xmind.net/download/win/ 官方快捷键说明:https://www.xmind.net/blog/cn/tag/xmind%EF%BC%8C%E5 ...

  2. ssh-copy-id命令解析

    ssh-copy-id命令可以把本地主机的公钥复制到远程主机的authorized_keys文件上, ssh-copy-id命令也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ssh/ ...

  3. Python3 tkinter基础 Entry show textvariable 密码输入框

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. vue学习【第三篇】:vue之node.js的简单介绍

    什么是node.js 它是可以运行JavaScript的服务平台,可以吧它当做一门后端程序,只是它的开发语言是JavaScript 安装node.js node.js的特性 - 非阻塞IO模型 - 时 ...

  5. 从0开始安装fedora23的笔记-- 以及使用fedora的常规问题-3

    关于sys的目录有: /etc/sys/, 和 /proc/sys fedora的桌面背景图片默认的在: /usr/share/backgrounds/, 里面有f23, gnome, images等 ...

  6. 1823: [JSOI2010]满汉全席 2-sat

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1823 思路 建图,缩点tarjan 判断impossible 代码 #include < ...

  7. 12.27 cf div3 解题报告

    12.27 cf div3 解题报告 wxy.wxy,带上分拉,全场做了个无脑小白 比赛场地 A: T1,跟着模拟就好了 B: sort一遍之后 去除的数一定是a[1]或者a[n] 比较去除谁小就输出 ...

  8. UVALive 7503 Change(乱搞)题解

    题意:你现在有面额为A的纸币,现在需要面额为B的钱(可以是一张也可以是好多张拼成一张),有一台自动售货机,里面有任意价格的商品,售货机兑换出的零钱是随机的(比如找你0.03可能给你0.01+0.01+ ...

  9. (转) GAN论文整理

    本文转自:http://www.jianshu.com/p/2acb804dd811 GAN论文整理 作者 FinlayLiu 已关注 2016.11.09 13:21 字数 1551 阅读 1263 ...

  10. ZooKeeper分布式过程协同技术详解1——ZooKeeper的概念和基础

    简介 分布式系统和应用,不仅能提供更强的计算能力,还能为我们提供更好的容灾性和扩展性. ZooKeeper是Google的Chubby项目的开源实现,它曾经作为Hadoop的子项目,在大数据领域得到广 ...