-
Notifications
You must be signed in to change notification settings - Fork 0
/
363.max_sum_rectangle_no_larger_k.go
54 lines (47 loc) · 1.19 KB
/
363.max_sum_rectangle_no_larger_k.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
package matrix
import "math"
/*
363. Max Sum of Rectangle No Larger Than K
https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/
*/
func buildPrefixSum(matrix [][]int) [][]int {
m, n := len(matrix), len(matrix[0])
result := [][]int {}
for i:=0; i<m; i++ {
result = append(result, make([]int, n+1))
}
for i:=0; i<m; i++ {
for j:=0; j<n; j++ {
result[i][j+1] = result[i][j] + matrix[i][j]
}
}
return result
}
func findRectangleSumClosestToK(prefixSum [][]int, k int) int {
m, n := len(prefixSum), len(prefixSum[0])
result := math.MinInt
for left := 0; left < n-1; left++ {
for right:=left; right < n-1; right++ {
for top:=0; top < m; top++ {
subresult := 0
for current := top; current < m; current++ {
subresult += prefixSum[current][right+1] - prefixSum[current][left]
if subresult == k {
return subresult
}
if subresult < k && subresult > result {
result = subresult
}
}
}
}
}
return result
}
func maxSumSubmatrix(matrix [][]int, k int) int {
prefixSum := buildPrefixSum(matrix)
return findRectangleSumClosestToK(prefixSum, k)
}
func MaxSumSubmatrix(matrix [][]int, k int) int {
return maxSumSubmatrix(matrix, k)
}