-
Notifications
You must be signed in to change notification settings - Fork 0
/
363.go
95 lines (86 loc) · 1.75 KB
/
363.go
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
package p363
import (
"math"
)
//363. Max Sum of Rectangle No Larger Than K
/**
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.
Example:
Given matrix = [
[1, 0, 1],
[0, -2, 3]
]
k = 2
The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2).
Note:
The rectangle inside the matrix must have an area > 0.
What if the number of rows is much larger than the number of columns?
*/
func max(a, b int) int {
if a > b {
return a
}
return b
}
func maxSumSubmatrix(matrix [][]int, k int) int {
if len(matrix) == 0 {
return 0
}
rows := len(matrix)
cols := len(matrix[0])
res := math.MinInt32
sums := make([]int, rows)
zeros := make([]int, rows)
sums2d := make([]int, rows+1)
helper := make([]int, rows+1)
for l := 0; l < cols; l++ {
copy(sums, zeros)
for r := l; r < cols; r++ {
sums2d[0] = 0
for i := 0; i < rows; i++ {
sums[i] += matrix[i][r]
sums2d[i+1] = sums2d[i] + sums[i]
}
ttm := mergeSort(sums2d, helper, k)
res = max(res, ttm)
}
}
return res
}
func mergeSort(sum, helper []int, k int) int {
if len(sum) <= 1 {
return math.MinInt32
}
mid := len(sum) / 2
ans := mergeSort(sum[:mid], helper, k)
if ans == k {
return k
}
ans = max(ans, mergeSort(sum[mid:], helper, k))
if ans == k {
return k
}
j, p := mid, mid
r := 0
//copy(helper, sum[:mid])
for i := 0; i < mid; i++ {
for j < len(sum) && sum[j]-sum[i] <= k {
j++
}
if j-1 >= mid {
ans = max(ans, sum[j-1]-sum[i])
if ans == k {
return k
}
}
for p < len(sum) && sum[p] < sum[i] {
helper[r] = sum[p]
r++
p++
}
helper[r] = sum[i]
r++
}
copy(sum, helper[:r])
return ans
}