Skip to content

Commit 89ab7a0

Browse files
authored
feat: add solutions to lc problem: No.0363 (doocs#1565)
No.0363.Max Sum of Rectangle No Larger Than K
1 parent af3d053 commit 89ab7a0

File tree

8 files changed

+2357
-1
lines changed

8 files changed

+2357
-1
lines changed

solution/0300-0399/0363.Max Sum of Rectangle No Larger Than K/README.md

Lines changed: 795 additions & 0 deletions
Large diffs are not rendered by default.

solution/0300-0399/0363.Max Sum of Rectangle No Larger Than K/README_EN.md

Lines changed: 787 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
4+
int m = matrix.size(), n = matrix[0].size();
5+
const int inf = 1 << 30;
6+
int ans = -inf;
7+
for (int i = 0; i < m; ++i) {
8+
vector<int> nums(n);
9+
for (int j = i; j < m; ++j) {
10+
for (int h = 0; h < n; ++h) {
11+
nums[h] += matrix[j][h];
12+
}
13+
set<int> ts;
14+
int s = 0;
15+
ts.insert(0);
16+
for (int x : nums) {
17+
s += x;
18+
auto it = ts.lower_bound(s - k);
19+
if (it != ts.end()) {
20+
ans = max(ans, s - *it);
21+
}
22+
ts.insert(s);
23+
}
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func maxSumSubmatrix(matrix [][]int, k int) int {
2+
m, n := len(matrix), len(matrix[0])
3+
const inf = 1 << 30
4+
ans := -inf
5+
for i := 0; i < m; i++ {
6+
nums := make([]int, n)
7+
for j := i; j < m; j++ {
8+
for h := 0; h < n; h++ {
9+
nums[h] += matrix[j][h]
10+
}
11+
s := 0
12+
rbt := redblacktree.NewWithIntComparator()
13+
rbt.Put(0, nil)
14+
for _, x := range nums {
15+
s += x
16+
if y, ok := rbt.Ceiling(s - k); ok {
17+
ans = max(ans, s-y.Key.(int))
18+
}
19+
rbt.Put(s, nil)
20+
}
21+
}
22+
23+
}
24+
return ans
25+
}
26+
27+
func max(a, b int) int {
28+
if a > b {
29+
return a
30+
}
31+
return b
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int maxSumSubmatrix(int[][] matrix, int k) {
3+
int m = matrix.length;
4+
int n = matrix[0].length;
5+
final int inf = 1 << 30;
6+
int ans = -inf;
7+
for (int i = 0; i < m; ++i) {
8+
int[] nums = new int[n];
9+
for (int j = i; j < m; ++j) {
10+
for (int h = 0; h < n; ++h) {
11+
nums[h] += matrix[j][h];
12+
}
13+
int s = 0;
14+
TreeSet<Integer> ts = new TreeSet<>();
15+
ts.add(0);
16+
for (int x : nums) {
17+
s += x;
18+
Integer y = ts.ceiling(s - k);
19+
if (y != null) {
20+
ans = Math.max(ans, s - y);
21+
}
22+
ts.add(s);
23+
}
24+
}
25+
}
26+
return ans;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from sortedcontainers import SortedSet
2+
3+
4+
class Solution:
5+
def maxSumSubmatrix(self, matrix: List[List[int]], k: int) -> int:
6+
m, n = len(matrix), len(matrix[0])
7+
ans = -inf
8+
for i in range(m):
9+
nums = [0] * n
10+
for j in range(i, m):
11+
for h in range(n):
12+
nums[h] += matrix[j][h]
13+
s = 0
14+
ts = SortedSet([0])
15+
for x in nums:
16+
s += x
17+
p = ts.bisect_left(s - k)
18+
if p != len(ts):
19+
ans = max(ans, s - ts[p])
20+
ts.add(s)
21+
return ans

0 commit comments

Comments
 (0)