-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMatrix.java
More file actions
127 lines (107 loc) · 2.58 KB
/
MaxMatrix.java
File metadata and controls
127 lines (107 loc) · 2.58 KB
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
import java.util.*;
public class MaxMatrix{
private int[][] arr;
private int[][] compress;
public MaxMatrix(int[][] _arr){
arr = _arr;
getCompress();
}
public int getResult(){
int max=0;
for (int i=0; i<compress.length;i++ ) {
int[][] result = process(compress[i]);
int v = getMaxSub(result,compress[i]);
max = Math.max(v,max);
}
return max;
}
private int[][] process(int[] temp){
int[][] result = new int[temp.length][2];
int N = temp.length;
Stack<LinkedList<Integer>> stack = new Stack<LinkedList<Integer>>();
LinkedList<Integer> curr;
boolean isEqual=false;
for (int i=0; i<N; i++) {
isEqual=false;
if(stack.isEmpty()){
curr = new LinkedList<Integer>();
curr.offerLast(i);
stack.push(curr);
}else{
while(!stack.isEmpty()){
curr = stack.peek();
if(temp[curr.peekLast()] < temp[i]){
break;
}else if(temp[curr.peekLast()] == temp[i]){
curr.offerLast(i);
isEqual=true;
break;
}else{
curr = stack.pop();
for (int v:curr ) {
result[v][0] = stack.isEmpty()? -1 :stack.peek().peekLast();
result[v][1] = i;
}
}
}
if(!isEqual){
curr = new LinkedList<Integer>();
curr.offerLast(i);
stack.push(curr);
}
}
}
while(!stack.isEmpty()){
curr = stack.pop();
for (int v:curr ) {
result[v][0] = stack.isEmpty()? -1 :stack.peek().peekLast();
result[v][1] = -1;
}
}
return result;
}
private int getMaxSub(int[][] temp,int[] org){
//第一行[x][y]:存放开始坐标
//第二行[x][y]:存放结束坐标
//第三行[2][0]:存放值
//int[][] result = new int[3][2];
int startX;
int startY;
int endX;
int endY;
int max=0;
for (int i=0; i<org.length;i++ ) {
int L = temp[i][0] == -1 ?0 :temp[i][0]+1;
int R = temp[i][1] == -1 ? org.length : temp[i][1];
max = Math.max(max,org[i]*(R-L));
}
return max;
}
private void getCompress(){
int N=arr.length;
int M = arr[0].length;
compress = new int[N][M];
for (int i=0; i<N; i++) {
compress[0][i] = arr[0][i];
}
for (int i=1; i<N; i++) {
//System.out.println();
for (int j=0; j<M;j++ ) {
compress[i][j] = arr[i][j] == 0 ? 0: compress[i-1][j] + 1;
//System.out.print(compress[i][j]+",");
}
}
}
public static void main(String[] args){
int[][] arr = new int[][]{
{1,1,0,1,1,0,1,1,0,1,1},
{1,1,0,1,1,1,1,1,0,1,1},
{1,1,1,1,1,1,1,1,1,1,0},
{1,1,0,1,1,1,1,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1,1},
};
MaxMatrix mm = new MaxMatrix(arr);
int max = mm.getResult();
System.out.println("max = "+max);
}
}