leetcode 最大矩形和

1.枚举法(超时)

 1 public class Solution {
 2     public int largestRectangleArea(int[] height) {
 3         int max=-1;
 4         for(int i=0;i<height.length;i++)
 5         {
 6             int len=1;
 7             int k=i-1;
 8             while(k>=0&&height[k]<=height[i])
 9             {
10                 k--;
11                 len++;
12             }
13             k=i+1;
14             while(k<height.length&&height[k]<=height[i])
15             {
16                 len++;
17                 k++;
18
19
20
21             }
22             if(max<len)
23             {
24                 max=len;
25             }
26
27
28
29             }
30             return max;
31
32
33
34
35
36
37     }
38 }

2.单调栈(AC),其实模拟了第一种方法,nyoj做过,效率为线性。写的代码有点长,但是很好理解

 1 class node
 2 {
 3     int d; //di
 4     int h;// higtht
 5 }
 6
 7
 8
 9 public class Solution {
10     public int largestRectangleArea(int[] height) {
11         int len=height.length;
12         if(len==0) return 0;
13         node n[]=new node[len];
14         for(int i=0;i<len;i++)
15         {
16             n[i]=new node();
17             n[i].d=1;
18             n[i].h=height[i];
19
20         }
21         Stack<node> s=new Stack<node>();
22         s.push(n[0]);
23         int max=n[0].h;
24         for(int j=1;j<len;j++)
25         {
26             node t=s.peek();
27             if(n[j].h>=t.h)
28             {
29                 s.push(n[j]);
30             }
31             else
32             {
33                 int with=0;
34                 while(!s.isEmpty()&&s.peek().h>n[j].h)
35                 {
36                    t=s.pop();
37                     with+=t.d;
38                     if(with*t.h>max) max=with*t.h;
39
40                 }
41                 n[j].d+=with;
42                 s.push(n[j]);
43
44              }
45
46
47
48         }
49         int with=0;
50         while(!s.isEmpty())
51         {
52             node t=s.pop();
53             with=with+t.d;
54             int temp=t.h*with;
55
56             if(temp>max) max=temp;
57
58         }
59
60 return max;
61     }}

3.最大01矩阵()。使用上述四项,枚举每一行。

 1 class node
 2 {
 3     int d; //di
 4     int h;// higtht
 5 }
 6
 7 public class Solution {
 8     public int largest(int[] height) {
 9         int len=height.length;
10         if(len==0) return 0;
11         node n[]=new node[len];
12         for(int i=0;i<len;i++)
13         {
14             n[i]=new node();
15             n[i].d=1;
16             n[i].h=height[i];
17
18         }
19         Stack<node> s=new Stack<node>();
20         s.push(n[0]);
21         int max=n[0].h;
22         for(int j=1;j<len;j++)
23         {
24             node t=s.peek();
25             if(n[j].h>=t.h)
26             {
27                 s.push(n[j]);
28             }
29             else
30             {
31                 int with=0;
32                 while(!s.isEmpty()&&s.peek().h>n[j].h)
33                 {
34                    t=s.pop();
35                     with+=t.d;
36                     if(with*t.h>max) max=with*t.h;
37
38                 }
39                 n[j].d+=with;
40                 s.push(n[j]);
41
42              }
43
44
45
46         }
47         int with=0;
48         while(!s.isEmpty())
49         {
50             node t=s.pop();
51             with=with+t.d;
52             int temp=t.h*with;
53
54             if(temp>max) max=temp;
55
56         }
57
58 return max;
59     }
60     public int maximalRectangle(char[][] matrix) {
61         if(matrix.length==0) return 0;
62
63        int len=matrix[0].length;
64        int ans[]=new int[len];
65        for(int i=0;i<len;i++)
66        {
67            ans[i]=matrix[0][i]-‘0‘;
68        }
69        int max=largest(ans);
70
71         for(int i=1;i<matrix.length;i++)
72         {
73            for(int j=0;j<matrix[0].length;j++)
74            {
75                if(matrix[i][j]==‘0‘) ans[j]=0;
76
77                else
78                {
79                    ans[j]+=1;
80                }
81
82
83
84            }
85
86           int t=largest(ans);
87           if(max<t) max=t;
88
89
90         }
91
92
93
94
95         return max;
96
97     }
98 }

https://oj.leetcode.com/problems/maximal-rectangle/

leetcode 最大矩形和,布布扣,bubuko.com

时间: 2024-07-30 03:25:32

leetcode 最大矩形和的相关文章

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

Leetcode 363. 矩形区域不超过 K 的最大数值和

//没有什么好想法,就用暴力遍历AC了class Solution { public: int maxSumSubmatrix(vector<vector<int>>& mat, int k) { int row = mat.size(); if(row ==0) return 0; int col = mat[0].size(); if(col==0) return 0; vector<vector<int>> dp(row + 1, vector

[LeetCode] 223.矩形面积

题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. 示例: 输入: -3, 0, 3, 4, 0, -1, 9, 2 输出: 45 说明: 假设矩形面积不会超出 int 的范围. 思路: 这道题,把问题考虑清楚就不难了! 首先,我们调整两个矩形,让第一个矩形是靠最左边的: 其次,

LeetCode 29. 矩形重叠 反向思维

题目描述 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们是否重叠并返回结果. 示例 1: 输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3] 输出:true 示例 2: 输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1] 输出:false 提示: 两个矩

Leetcode 836. 矩形重叠

矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们是否重叠并返回结果. 示例 1: 输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3] 输出:true 示例 2: 输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1] 输出:false 提示: 两个矩形 rec

LeetCode:Rectangle Area - 矩形交叉部分的面积

1.题目名称 Rectangle Area(矩形交叉部分的面积) 2.题目地址 https://leetcode.com/problems/rectangle-area/ 3.题目内容 英文:Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown

(每日算法)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

解题报告:LeetCode Largest Rectangle in Histogram(计算最大矩形面积)

题目出处:https://leetcode.com/problems/largest-rectangle-in-histogram/题意描述:给定n个非负的整数,代表n个依次相邻的宽度为1的柱形的高,求这些柱形所能形成的最大的矩形面积. 解决思路:此题最直接最原始的做法就是扫描起点和终点,并随时更新最大面积,但是这样的做法的复杂度为O(n^2),显然会超时,这里就不再贴代码了. 于是我们需要考虑怎么将复杂度降下来,一种想法是在求面积之前进行预处理,将每个整数左右的第一个比当前位置矮的柱形的下标l

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