-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximumSubgrid.java
129 lines (110 loc) · 4.33 KB
/
MaximumSubgrid.java
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
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class MaximumSubgrid {
private static int subSum(int i1, int j1, int i2, int j2, int[][] sum){
int total = sum[i2][j2];
if (j1 - 1 >= 0)
total -= sum[i2][j1 - 1];
if (i1 - 1 >= 0)
total -= sum[i1 - 1][j2];
if (i1 - 1 >= 0 && j1 - 1 >= 0)
total += sum[i1 - 1][j1 - 1];
return total;
}
public static void printSubtract(int i, int j, int i1, int j2, List<List<Integer>> grid){
for(int kk = i; kk <= i1; kk++){
for(int k = j; k <= j2; k++){
System.out.print(grid.get(kk).get(k)+" ");
}
System.out.println();
}
}
private static int linearized(int k, int[][] sum, int maxSum){
int n = sum.length;
int thisSum = 0;
int ans = -1;
boolean spoilt = false;
for(int i = 0; i < (n * n); i++){
int row = (i / n);
int column = (i % n);
int end = row + k - 1;
int endColumn = column + k - 1;
if(end < n && endColumn < n){
thisSum = subSum(row, column, end, endColumn, sum);
if(thisSum <= maxSum){
ans = thisSum;
} else{
spoilt = true;
break;
}
}
}
return spoilt ? -1 : ans;
}
private static int doSearch(int low, int high, int[][] sum, int maxSum, int currentMax, List<List<Integer>> grid) throws IOException{
if(low >= high){
return currentMax;
}
int mid = (low + high) >> 1;
int containsValida = linearized(mid,sum, maxSum);
if(containsValida == -1){
return doSearch(low, mid, sum, maxSum, currentMax, grid);
} else {
return doSearch(mid + 1, high, sum, maxSum, mid, grid);
}
}
public static int largestSubgrid(List<List<Integer>> grid, int maxSum) throws IOException{
int[][] sum = new int[grid.size()][grid.size()];
sum[0][0] = grid.get(0).get(0);
boolean hasPossibility = grid.get(0).get(0) <= maxSum;
for(int i = 1; i < sum.length; i++){
int current_column = grid.get(0).get(i);
int current_row = grid.get(i).get(0);
sum[0][i] = current_column + sum[0][i-1];
sum[i][0] = current_row + sum[i - 1][0];
if(Math.min(current_row, current_column) > maxSum){
hasPossibility = false;
}
}
for(int i = 1; i < sum.length; i++){
for(int j = 1; j < sum.length;j++){
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + grid.get(i).get(j) - sum[i - 1][j - 1];
if(grid.get(i).get(j) > maxSum){
hasPossibility = false;
}
}
}
if(sum[sum.length - 1][sum.length - 1] <= maxSum){
return sum.length;
}
int valid = doSearch(2, sum.length , sum, maxSum, 0, grid);
return hasPossibility ? (valid == 0 ? 1 : valid) : 0;
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("matrix.in"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("matrix.out"));
int gridRows = Integer.parseInt(bufferedReader.readLine().trim());
int gridColumns = Integer.parseInt(bufferedReader.readLine().trim());
List<List<Integer>> grid = new ArrayList<>();
IntStream.range(0, gridRows).forEach(i -> {
try {
grid.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList())
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
int maxSum = Integer.parseInt(bufferedReader.readLine().trim());
int result = largestSubgrid(grid, maxSum);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}