-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_sum_submatrix.cc
54 lines (50 loc) · 1.14 KB
/
max_sum_submatrix.cc
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
// Program to find the maximum sum submatrix in a given NxN matrix.
// Time: O(n^3)
// Space: O(n^2)
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int FindMaxAndDistance(int* arr, int n) {
int max_sum = arr[0], run_sum = arr[0];
for (int i = 1; i < n; ++i) {
run_sum = max(run_sum + arr[i], arr[i]);
max_sum = max(max_sum, run_sum);
}
return max_sum;
}
int main() {
int n;
cin >> n;
int a[n][n];
int tmp_arr[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> a[i][j];
tmp_arr[i][j] = a[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 1; j < n; ++j) {
tmp_arr[i][j] += tmp_arr[i][j-1];
}
}
int max_sum = INT_MIN;
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
int tmp[n];
if (i == j) {
for (int k = 0; k < n; ++k) {
tmp[k] = a[k][i];
}
} else {
for (int k = 0; k < n; ++k) {
tmp[k] = tmp_arr[k][j] - tmp_arr[k][i] + a[k][i];
}
}
max_sum = max(max_sum, FindMaxAndDistance(tmp, n));
}
}
cout << max_sum << endl;
return 0;
}