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,left=i,right=i;
                while(left>0&&heights[--left]>=heights[i]) len++;//向左延展
                while(right<heights.length-1&&heights[++right]>=heights[i]) len++; //向右延展
                ans=Math.max(ans,heights[i]*len); //更新最大值
        }
        return ans;
    }

这个方法效率不是很高,但是是最简单,也是最容易理解的。、

85--最大矩形(Maximal Rectangle)

这一题的思路与上一题相同。主要是第一步。
首先我们先建立一个大小和Matrix相同的大小的二维数组map

        int Y=matrix.length;
        int X=matrix[0].length;
        int[][] map=new int[Y][X];
        for(int y=0;y<Y;y++) {
            for(int x=0;x<X;x++) {
                if(matrix[y][x]=='1') {
                /*假如matrix[y][x]=='1',就把当前位置的map值,加上map[y-1][x]的值,如
                000       000
                111  -》 111
                101       202*/
                    if(y>0) {
                        map[y][x]=map[y-1][x]+1;
                    }else {
                        map[y][x]=1;
                    }
                }
            }
        }

通过向下累加的方式,更新map,然后在对每一行的map[y],进行如84题的操作,就可以得出最大的面积

    public int maximalRectangle(char[][] matrix) {
        if(matrix.length==0||matrix[0].length==0) return 0;
        int Y=matrix.length;
        int X=matrix[0].length;
        int[][] map=new int[Y][X];
        for(int y=0;y<Y;y++) {
            for(int x=0;x<X;x++) {
                if(matrix[y][x]=='1') {
                    if(y>0) {
                        map[y][x]=map[y-1][x]+1;
                    }else {
                        map[y][x]=1;
                    }
                }
            }
        }
        int ans=0;
        //这里和84题一样
        for(int[] nums:map) {
            for(int i=0;i<nums.length;i++) {
                int len=1,left=i,right=i;
                while(left>0&&nums[--left]>=nums[i]) len++;
                while(right<nums.length-1&&nums[++right]>=nums[i]) len++;
                ans=Math.max(ans,nums[i]*len);
            }
        }
        return ans;
    }

就是这样

原文地址:https://www.cnblogs.com/duangL/p/11575114.html

时间: 2024-10-13 18:39:45

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

(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)

思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解.比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形. 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

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

leetCode 84.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 largest

[LeetCode] 84. 柱状图中最大的矩形

题目链接 : https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ 题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]. 图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位. 示例: 输入: [2,1,5,6,2,3] 输出:

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: Largest Rectangle in Histogram [084]

[题目] 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 lar

LeetCode (85): Maximal Rectangle [含84题分析]

链接: https://leetcode.com/problems/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. [中文描述] 给一个二维数组, 算出里面最大的全1矩形面积,比如: [ ['1','1','1','0'], ['1','1','1','1']

[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 return its area. Example: Input: [ ["1","0","1","0","0"], ["1","0","1",&qu

【leetcode刷题笔记】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 largest