-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxSubMatrix.cpp
More file actions
32 lines (26 loc) · 827 Bytes
/
maxSubMatrix.cpp
File metadata and controls
32 lines (26 loc) · 827 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int maxSubMatrix(vector<vector<int> > matrix,
int n, int m)
{
int base_sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
base_sum += matrix[i][j];
int result = 0;
for (int i = 0; i+n < matrix.size(); i++) {
if (i > 1)
for (int y = 0; y < m; y++)
base_sum += (matrix[i+n][y] - matrix[i-1][y]);
int real_sum = base_sum;
if (real_sum > result)
result = real_sum;
for (int j = 0; j+m < matrix[0].size(); j++)
if (j > 1)
for (int x = 0; x < n; x++)
real_sum += matrix[x][j+m] - matrix[x][j-1];
if (real_sum > result)
result = real_sum;
}
return result;
}