-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0085-MaximalRectangle.cs
104 lines (98 loc) · 2.58 KB
/
0085-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
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
using System;
using Xunit;
using Util;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace MaximalRectangle
{
public class Solution
{
public int MaximalRectangle(char[][] matrix)
{
if (matrix.Length == 0)
{
return 0;
}
int max = 0;
int row = matrix.Length;
int col = matrix[0].Length;
int total = row * col;
for (int i = 0; i < total; i++)
{
var x = Rectangle(matrix, row, col, i);
max = Math.Max(max, x);
}
return max;
}
int Rectangle(char[][] matrix, int row, int col, int cell)
{
int r = cell / col;
int c = cell % col;
if (matrix[r][c] == '0')
{
return 0;
}
int w = 1;
for (int i = c + 1; i < col; i++)
{
if (matrix[r][i] == '0')
{
break;
}
w++;
}
int h = 1;
for (int i = r + 1; i < row; i++)
{
if (matrix[i][c] == '0')
{
break;
}
h++;
}
int max = 0;
for (int i = r + 1; i < r + h; i++)
{
for (int j = c + 1; j < c + w; j++)
{
if (matrix[i][j] == '0')
{
var a1 = (i - r) * w;
max = Math.Max(max, a1);
w = j - c;
break;
}
}
}
return Math.Max(max, w * h);
}
}
public class Test
{
static void Verify(char[][] matrix, int exp)
{
Console.WriteLine($"{matrix.Char2dToJson('"')}");
int res;
using (new Timeit())
{
res = new Solution().MaximalRectangle(matrix);
}
Assert.Equal(exp, res);
}
static public void Run()
{
Console.WriteLine(typeof(Solution).Namespace);
var lines = "0085-data.txt".InputFromFile();
char[][] matrix;
int exp;
int idx = 0;
while (idx < lines.Length)
{
matrix = lines[idx++].JsonToChar2d('"');
exp = int.Parse(lines[idx++]);
Verify(matrix, exp);
}
}
}
}