-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode Problem 85 Maximal Rectangle.txt
133 lines (110 loc) · 3.83 KB
/
Leetcode Problem 85 Maximal Rectangle.txt
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6
Hint to solve: Use dynamic programming.
Go through each row. If the current cell value is non zero add the previous row value.
Calculate the max area of a rectange of a histogram for each row.
public class Solution
{
public int CalculateMaxAreaHistogram(int[] row)
{
if(row.Count() == 0)
return 0;
if(row.Count() == 1)
return row[0];
int maxAreaHistogram = 0;
Stack<int> heightStack = new Stack<int>();
Stack<int> positionStack = new Stack<int>();
heightStack.Push(row[0]);
positionStack.Push(0);
for(int i = 1; i<row.Count(); ++i)
{
if(heightStack.Count() == 0 && positionStack.Count() == 0)
{
heightStack.Push(row[i]);
positionStack.Push(i);
}
else if(row[i] > heightStack.Peek())
{
heightStack.Push(row[i]);
positionStack.Push(i);
}
else if(row[i] < heightStack.Peek())
{
int temp_position = positionStack.Peek();
while(heightStack.Count() > 0 && positionStack.Count() > 0 &&
heightStack.Peek() > row[i])
{
int area = heightStack.Peek() * (i - positionStack.Peek());
temp_position = positionStack.Peek();
heightStack.Pop();
positionStack.Pop();
if(area > maxAreaHistogram)
maxAreaHistogram = area;
}
heightStack.Push(row[i]);
positionStack.Push(temp_position);
}
}
while(heightStack.Count() > 0 && positionStack.Count() > 0)
{
int area = heightStack.Peek() * (row.Count() - positionStack.Peek());
heightStack.Pop();
positionStack.Pop();
if(area > maxAreaHistogram)
maxAreaHistogram = area;
}
// Console.WriteLine("For row: ");
// foreach(var cell in row)
// {
// //Console.Write(cell + " ");
// }
// //Console.WriteLine("maxArea: "+ maxAreaHistogram);
return maxAreaHistogram;
}
public int MaximalRectangle(char[][] matrix)
{
int maxArea = 0;
int[][] grid = new int[matrix.Count()][];
for(int i = 0; i<matrix.Count(); ++i)
{
grid[i] = new int[matrix[i].Count()];
for(int j = 0; j<matrix[i].Count(); ++j)
{
grid[i][j] = matrix[i][j] - 48;
}
}
//Go through each row in the matrix
for(int row = 0 ; row < grid.Count(); ++row)
{
int[] currentGridRow = grid[row];
if(row > 0)
{
for(int col = 0; col < grid[row].Count(); ++col)
{
if(grid[row][col] == 0)
{
continue;
}
else
{
currentGridRow[col] += grid[row-1][col];
}
}
}
//For current row calculate the area of a histogram
int maxAreaHistogram = CalculateMaxAreaHistogram(currentGridRow);
if(maxAreaHistogram > maxArea)
maxArea = maxAreaHistogram;
}
return maxArea;
}
}