-
Notifications
You must be signed in to change notification settings - Fork 112
/
085-MaximalRectangle.cs
50 lines (43 loc) · 1.55 KB
/
085-MaximalRectangle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//-----------------------------------------------------------------------------
// Runtime: 120ms
// Memory Usage: 28.6 MB
// Link: https://leetcode.com/submissions/detail/378115442/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _085_MaximalRectangle
{
public int MaximalRectangle(char[][] matrix)
{
if (matrix.Length == 0) return 0;
int N = matrix.Length;
int M = matrix[0].Length;
int[] heights = new int[M];
var maxArea = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;
maxArea = Math.Max(maxArea, ComputeLargestRectangle(heights));
}
return maxArea;
}
private int ComputeLargestRectangle(int[] heights)
{
var stack = new Stack<int>();
stack.Push(-1);
int maxArea = 0;
for (int i = 0; i < heights.Length; i++)
{
while (stack.Peek() != -1 && heights[stack.Peek()] >= heights[i])
maxArea = Math.Max(maxArea, heights[stack.Pop()] * (i - stack.Peek() - 1));
stack.Push(i);
}
while (stack.Peek() != -1)
maxArea = Math.Max(maxArea, heights[stack.Pop()] * (heights.Length - stack.Peek() - 1));
return maxArea;
}
}
}