最大的矩形面积 Maximal Rectangle

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)。

核心的思路是,寻找每个直方图的左边和右边第一个比他小的位置,然后计算其能达到的面积最大值,最后取max即可。

    public int largestRectangleArea(int[] heights) {
        if (heights.length == 0) return 0;
        int res = 0;
        Stack<Integer> stack = new Stack<>();
        int idx, leftMost, rightMost;
        for (int i = 0; i < heights.length; i++) {
            if (stack.isEmpty() || heights[i] >= heights[stack.peek()]) stack.push(i);
            else {
                rightMost = i;
                idx = stack.pop();
                while (!stack.isEmpty() && heights[stack.peek()] == heights[idx]) idx = stack.pop();
                leftMost = stack.isEmpty() ? -1 : stack.peek();
                res = Math.max(res, heights[idx] * (rightMost - leftMost - 1));
                i--;
            }
        }
        rightMost = stack.isEmpty() ? 0 :stack.peek() + 1;
        while (!stack.isEmpty()) {
            idx = stack.pop();
            leftMost = stack.isEmpty() ? -1 : stack.peek();
            res = Math.max(res, heights[idx] * (rightMost - leftMost - 1));
        }
        return res;
    }

二、Maximal Rectangle

问题描述:

问题求解:

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

    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];
        int res = 0;
        for (int i = 0; i < n; i++) height[0][i] = matrix[0][i] - ‘0‘;
        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];
            }
        }
        for (int i = 0; i < m; i++) {
            res = Math.max(res, fastHelper(height[i]));
        }
        return res;
    }

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

    private int fastHelper(int[] nums) {
        int res = 0;
        int idx, leftMost, rightMost;
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums.length; i++) {
            if (stack.isEmpty() || nums[i] >= nums[stack.peek()]) stack.push(i);
            else {
                rightMost = i;
                idx = stack.pop();
                while (!stack.isEmpty() && nums[idx] == nums[stack.peek()]) idx= stack.pop();
                leftMost = stack.isEmpty() ? -1 : stack.peek();
                res = Math.max(res, nums[idx] * (rightMost - leftMost - 1));
                i--;
            }
        }
        rightMost = stack.isEmpty() ? 0 : stack.peek() + 1;
        while (!stack.isEmpty()) {
            idx = stack.pop();
            while (!stack.isEmpty() && nums[idx] == nums[stack.peek()]) idx= stack.pop();
            leftMost = stack.isEmpty() ? -1 : stack.peek();
            res = Math.max(res, nums[idx] * (rightMost - leftMost - 1));
        }
        return res;
    }

原文地址:https://www.cnblogs.com/TIMHY/p/9650229.html

时间: 2024-10-11 03:27:34

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

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

84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度len,最后把len*heights[i] 就是延展开的面积,最后做比对,得出最大. public int largestRectangleArea(int[] heights) { int ans=0; for(int i=0;i<heights.length;i++) { int len=1,lef

LeetCode 223. 矩形面积(Rectangle Area)

223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode223. Rectangle Area中等 示例: 输入: -3, 0, 3, 4, 0, -1, 9, 2 输出: 45 说明: 假设矩形面积不会超出 int 的范围. Java 实现 class Solution { public int computeArea(int A, int B, int

C++]LeetCode: 133 Largest Rectangle in Histogram(最大矩形面积)

题目: Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The larg

【最小矩形面积覆盖:凸包+旋转卡壳】UVA 10173 Smallest Bounding Rectangle

[最小矩形面积覆盖:凸包+旋转卡壳]UVA 10173 Smallest Bounding Rectangle 题目链接:UVA 10173 Smallest Bounding Rectangle 题目大意 给你n个点,求能够覆盖所有点集的最小矩形面积. 笔者的第2道凸包题目,凸包 + 旋转卡壳,实现点集的最小矩形面积覆盖问题 ">=0"写成"<=0"坑了我一下午!QAQ 说一下思路 ①Graham's Scan法构建凸包,时间复杂度O(nlogn) ②

Leetcode:Largest Rectangle in Histogram 最大矩形面积

Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given heigh

[LeetCode] 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. 此题是之前那道的Largest Rectangle in Histogram 直方图中最大的矩形 的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用Largest Rectangle in Hist

[LeetCode] Rectangle Area 矩形面积

Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. Cre

[HDU 4419] Colourful Rectangle (扫描线 矩形面积并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题就是维护每个颜色的长度,写起来很蛋疼. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6

Poj 2559 Largest Rectangle in a Histogram(柱形统计图中的最大矩形面积)

 给出一个柱形统计图中,求其中的最大矩形面积 做完这道题,搜了一下题解大部分基本都是单调栈......然而做之前并不知道这是什么,其实用递推也可以做这道题,理解起来比较容易. 用两个数组l,r记录当前坐标可以向左和向右延伸的最远位置的坐标,然后就是递推了. 初始时将l[i],r[i]的值置为i,即自己的坐标.这里拿l[i]举例: 从左向右扫描统计图,计算当前位置的l[i]时,如果h[i] > h[ l[i] - 1 ]的话,那么l[i] = l[ l[i]-1  ]. 然后对于每个位置,an