-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubMatrixSum.java
More file actions
201 lines (137 loc) · 3.68 KB
/
SubMatrixSum.java
File metadata and controls
201 lines (137 loc) · 3.68 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.util.*;
public class SubMatrixSum{
private int[][] arr;
private int[][] compress;
public SubMatrixSum(int[][] _arr){
arr = _arr;
getCompress();
}
private void getCompress(){
int N=arr.length;
int M = arr[0].length;
compress = new int[N][M];
for (int i=0; i<M; 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 int getSubResult(int[] temp){
int[][] result=getArray(temp);
int sum = 0;
// for (int i=0; i<result.length;i++ ) {
// for (int j=0; j<result[i].length; j++) {
// System.out.print(result[i][j]+",");
// }
// System.out.println();
// }
for (int i=0; i<result.length;i++ ) {
if(result[i][1]==result[i][0]&&result[i][0]==0) continue;
int size = (result[i][1]==-1 ? temp.length : result[i][1]) - result[i][0];
int low = Math.max(result[i][0]==-1?0:temp[result[i][0]],result[i][1]==-1?0:temp[result[i][1]]);
int high = temp[i];
sum+=getNum(low,high,size-1);
}
return sum;
}
public int getResult(){
int sum = 0;
for (int i=0; i<compress.length;i++ ) {
sum+= getSubResult(compress[i]);
}
return sum;
}
private int[][] getArray(int[] temp){
Stack<Integer> stack = new Stack<Integer> ();
int[][] result = new int[temp.length][2];
for (int i=0; i<temp.length; i++) {
if(stack.isEmpty()){
stack.push(i);
}else{
while(!stack.isEmpty()){
if(temp[stack.peek()] < temp[i]){
break;
}
int cur = stack.pop();
if(temp[cur] != temp[i]){
result[cur][0]= stack.isEmpty()?-1:stack.peek();
result[cur][1]= i;
}
}
stack.push(i);
}
}
while(!stack.isEmpty()){
int cur = stack.pop();
result[cur][0]= stack.isEmpty()?-1:stack.peek();
result[cur][1]= -1;
}
return result;
}
private int getNum(int start,int end,int length){
//System.out.println("start="+start+",end="+end+",length="+length);
return (length*(length+1)/2) * (end-start);
}
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},
{0,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,0,1},
};
SubMatrixSum ss = new SubMatrixSum(arr);
System.out.println("Sum = "+ss.getResult());
System.out.println("Sum = "+numSubmat(arr));
}
public static int numSubmat(int[][] mat) {
if (mat == null || mat.length == 0 || mat[0].length == 0) {
return 0;
}
int nums = 0;
int[] height = new int[mat[0].length];
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
height[j] = mat[i][j] == 0 ? 0 : height[j] + 1;
}
nums += countFromBottom(height);
}
return nums;
}
public static int countFromBottom(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int nums = 0;
int[] stack = new int[height.length];
int si = -1;
for (int i = 0; i < height.length; i++) {
while (si != -1 && height[stack[si]] >= height[i]) {
int cur = stack[si--];
if (height[cur] > height[i]) {
int left = si == -1 ? -1 : stack[si];
int n = i - left - 1;
int down = Math.max(left == -1 ? 0 : height[left], height[i]);
nums += (height[cur] - down) * num(n);
}
}
stack[++si] = i;
}
while (si != -1) {
int cur = stack[si--];
int left = si == -1 ? -1 : stack[si];
int n = height.length - left - 1;
int down = left == -1 ? 0 : height[left];
nums += (height[cur] - down) * num(n);
}
return nums;
}
public static int num(int n) {
return ((n * (1 + n)) >> 1);
}
}