-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMaximumSumRectangle.java
51 lines (49 loc) · 1.54 KB
/
MaximumSumRectangle.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
/*https://practice.geeksforgeeks.org/problems/maximum-sum-rectangle2948/1*/
class Solution {
int maximumSumRectangle(int R, int C, int M[][]) {
// code here
int[][] preSum = new int[R+1][C];
int i, j, rs, re, col, maxSum = Integer.MIN_VALUE, sum;
for (i = 1; i <= R; ++i)
for (j = 0; j < C; ++j)
preSum[i][j] = preSum[i-1][j]+M[i-1][j];
for (rs = 0; rs < R; ++rs)
{
for (re = rs+1; re <= R; ++re)
{
sum = 0;
for (col = 0; col < C; ++col)
{
sum += preSum[re][col]-preSum[rs][col];
if (sum > maxSum) maxSum = sum;
else if (sum < 0) sum = 0;
}
}
}
return maxSum;
}
}
class Solution {
int maximumSumRectangle(int R, int C, int M[][]) {
// code here
int[][] preSum = new int[R][C+1];
int i, j, rs, re, col, maxSum = Integer.MIN_VALUE, sum;
for (i = 1; i <= C; ++i)
for (j = 0; j < R; ++j)
preSum[j][i] = preSum[j][i-1]+M[j][i-1];
for (rs = 0; rs < C; ++rs)
{
for (re = rs+1; re <= C; ++re)
{
sum = 0;
for (col = 0; col < R; ++col)
{
sum += preSum[col][re]-preSum[col][rs];
if (sum > maxSum) maxSum = sum;
else if (sum < 0) sum = 0;
}
}
}
return maxSum;
}
}