GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/MaximalRectangle.java at master · Tulpen/Leetcode-Java

Expand file tree

/

MaximalRectangle.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
76 lines (65 loc) · 2.46 KB
/

MaximalRectangle.java

Copy path

File metadata and controls

76 lines (65 loc) · 2.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public abstract class MaximalRectangle {
// 耗时10ms,时间复杂度是O(mn),空间复杂度O(n)
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0) {
return 0;
}
int row = matrix.length, col = matrix[0].length, max = 0;
int[] heights = new int[col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] == '0') {
heights[j] = 0;
} else {
heights[j]++;
}
}
max = Math.max(max, largestRectangleArea(heights));
}
return max;
}
// 耗时9ms
public int maximalRectangle2(char[][] matrix) {
if (matrix.length == 0) {
return 0;
}
int row = matrix.length, col = matrix[0].length, area = 0;
int[] height = new int[col], left = new int[col], right = new int[col];
for (int i = 0; i < col; right[i] = col - 1, i++);
for (int i = 0; i < row; i++) {
int leftStart = 0, rightStart = col - 1;
for (int j = 0; j < col; j++) {
if (matrix[i][j] == '0') {
height[j] = 0;
} else {
height[j]++;
}
}
// 注意当matrix[i][j]为0时其实left[j]和right[j]是多少已经不重要了,因为
// height[j]为0,所以这里算left[j]和right[j]只是为了方便计算下一层
// 所以left[j]=0好理解,而right[j]=col-1的目的是为了设置成最大的,因为下一层要求min
for (int j = 0; j < col; j++) {
if (matrix[i][j] == '0') {
left[j] = 0;
leftStart = j + 1;
} else {
left[j] = Math.max(left[j], leftStart);
}
}
for (int j = col - 1; j >= 0; j--) {
// 注意这里不是0,而是字符'0'
if (matrix[i][j] == '0') {
right[j] = col - 1;
rightStart = j - 1;
} else {
right[j] = Math.min(right[j], rightStart);
}
}
for (int j = 0; j < col; j++) {
area = Math.max(area, (right[j] - left[j] + 1) * height[j]);
}
}
return area;
}
abstract int largestRectangleArea(int[] heights);
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.