-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlargestSubgrid.cpp
366 lines (206 loc) · 8.48 KB
/
largestSubgrid.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Solution by Rahul Surana
***********************************************************
Given a square grid of integers and an integer value, maxSum ,
determine the maximum size of the square sub-grid where for all such sub-grids, the sum of all its elements' values is less than or equal to the value maxSum .
Example:
grid =[[2, 2, 2], [3, 3, 3], [4, 4, 4]]
maxSum: different scenarios shown below
maxSum: The maximum sum of all square sub-grids of a size must be less than or equal to this integer value
1. The maximum 1x1 grid has a sum of 4. If maxSum < 4 there is no size square sub-grid that satisfies the condition. The answer is 0.
2. The maximum 2x2 grid has a sum of 14. If 4 ≤ maxSum < 14 , the maximum size of the square sub-grid is 1.
3. The maximum 3x3 grid has a sum of 27. If 14 ≤ maxSum < 27 , the maximum size of the square sub-grid is 2.
4. If maxSum ≥ 27 , the entire grid satisfies the condition so the answer is 3 .
Function Description
Complete the function largestSubgrid in the editor below.
largestSubgrid has the following parameter(s):
int grid[n][n]: an n × n array where grid[i][j] is the value of the cell in the i<sup>th</sup> row and j<sup>th</sup> column
int maxSum: an integer, the maximum acceptable sum of any sub-grid
Returns:
int: an integer that denotes the largest integer k such that there is no k × k sub-grid with a total value greater than maxSum . If all square sub-grids have value greater than maxSum , the function should return 0.
Constraints
<li> 1 ≤ n ≤ 1550
<li> 1 ≤ maxSum ≤ 10<sup>9</sup>
<li> 1 ≤ grid[i][j] ≤ 10<sup>7</sup>
<li>the sum of any entire grid is ≤ 10<sup>9</sup>
<!-- <StartOfInputFormat> DO NOT REMOVE THIS LINE-->
<summary class="section-title">Input Format for Custom Testing</summary>
<div class="collapsable-details">
Input from stdin will be processed as follows and passed to the function .
In the first line, there is a single integer n representing the number of rows in grid .
In the second line, the integer n is repeated and represents the number of columns in grid .
Each of the next n lines contains a row, grid[i] , containing n space-separated integers, each representing a value of grid[i][j] .
In the last line, there is a single integer maxSum .
<!-- </StartOfInputFormat> DO NOT REMOVE THIS LINE-->
Sample Case 0
<div class="collapsable-details">
<p class="section-title">Sample Input 0
STDIN Function
----- --------
</strong>3 → grid[n][n] n = 3
3 </strong>
1 1 1 → grid = [[1,1,1], [1,1,1], [1,1,1]]
1 1 1
1 1 1
4 → </strong> maxSum = 4
<p class="section-title">Sample Output 0
2
<p class="section-title">Explanation 0
n = 3
grid = [[1,1,1], [1,1,1], [1,1,1]]
maxSum = 4 .
<li>Square grid of 3 rows and 3 columns :
<li>Each square sub-grid of size 1 has a sum of values = 1
<li>Each square sub-grid of size 2 has a sum of values = 4
<li>The square sub-grid of size 3 has a sum of values = 9
The maximum size of the appropriate sub-grid ( 4 <= 4).
<summary class="section-title">Sample Case 1</summary>
<div class="collapsable-details">
<p class="section-title">Sample Input 1
STDIN Function
----- -------- </strong>
4 → grid[n][n] n = 4
4
1 1 1 1 → grid = [[1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4]]
2 2 2 2
3 3 3 3
4 4 4 4
39 → </strong>maxSum = 39
<p class="section-title">Sample Output 1
3
<p class="section-title">Explanation 1
n = 4
grid = [[1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4]]
maxSum = 39 .
<li>Square grid of 4 rows and 4 columns :
<li>A square sub-grid of size 1 has a maximum sum of values = 4
<li>A square sub-grid of size 2 has a maximum sum of values = 14
<li>A square sub-grid of size 3 has a maximum sum of values = 27
<li>A square sub-grid of size 4 has a maximum sum of values = 40
The maximum size of appropriate sub-grid is 3, (27 < 39).
<summary class="section-title">Sample Case 2</summary>
<div class="collapsable-details">
<p class="section-title">Sample Input 2
STDIN Function
----- -------- </strong>
2 → grid[n][n] n = 2
2 </strong>
4 5 → grid = [[4,5], [6,7]]
6 7
2 → </strong> maxSum = 2
<p class="section-title">Sample Output 2
0
<p class="section-title">Explanation 2
n = 2
grid = [[4,5],[6,7]]
maxSum = 2
<li>Square grid of 2 rows and 2 columns :
<li>A square sub-grid of size 1 has a maximum sum of 7.
<li>A square sub-grid of size 2 has a maximum sum of 22.
Any size sub-grid has a maximum sum > 2.
***********************************************************
*/
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
/*
* Complete the 'largestSubgrid' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. 2D_INTEGER_ARRAY grid
* 2. INTEGER maxSum
*/
int largestSubgrid(vector<vector<int>> grid, int maxSum) {
int z = 0;
int ans = 0;
vector<vector<int>> ss(grid.size(),vector<int>(grid[0].size(),0));
ss[0][0] = grid[0][0];
for(int i = 1 ; i < grid.size(); i++){
ss[0][i] = grid[0][i] + ss[0][i-1];
}
for(int i = 1 ; i < grid[0].size(); i++){
ss[i][0] = grid[i][0] + ss[i-1][0];
}
for(int i = 1; i < grid.size(); i++){
for(int j = 1; j < grid[0].size(); j++){
ss[i][j] = grid[i][j]+ss[i-1][j]+ss[i][j-1]- ss[i-1][j-1];
}
}
int f = 0;
while(z < maxSum && f < min(grid.size(),grid[0].size())){
for(int i = 0 ; i < grid.size()-f; i++){
for(int j = 0 ; j < grid[0].size()-f; j++){
int q = ss[i+f][j+f];
if(i+f < grid.size()) q -= ss[i+f][j];
if(j+f <grid[0].size()) q -= ss[i][j+f];
if(i+f <grid.size() && j+f <grid[0].size()) q+= ss[i][j];
z = max(z,q);
if(z>maxSum) break;
}
}
if(z > maxSum) return f-1;
if(z == maxSum) return f;
ans++;
// cout << z <<" "<<f <<"\n";
f++;
}
return min(grid.size(),grid[0].size())-1;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string grid_rows_temp;
getline(cin, grid_rows_temp);
int grid_rows = stoi(ltrim(rtrim(grid_rows_temp)));
string grid_columns_temp;
getline(cin, grid_columns_temp);
int grid_columns = stoi(ltrim(rtrim(grid_columns_temp)));
vector<vector<int>> grid(grid_rows);
for (int i = 0; i < grid_rows; i++) {
grid[i].resize(grid_columns);
string grid_row_temp_temp;
getline(cin, grid_row_temp_temp);
vector<string> grid_row_temp = split(rtrim(grid_row_temp_temp));
for (int j = 0; j < grid_columns; j++) {
int grid_row_item = stoi(grid_row_temp[j]);
grid[i][j] = grid_row_item;
}
}
string maxSum_temp;
getline(cin, maxSum_temp);
int maxSum = stoi(ltrim(rtrim(maxSum_temp)));
int result = largestSubgrid(grid, maxSum);
fout << result << "\n";
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}