Skip to content

Commit 5a3a475

Browse files
committed
2025-01-09 v. 7.8.5: added "363. Max Sum of Rectangle No Larger Than K"
1 parent cca2f65 commit 5a3a475

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
654654
| 239. Sliding Window Maximum | [Link](https://leetcode.com/problems/sliding-window-maximum/) | [Link](./lib/hard/239_sliding_window_maximum.rb) | [Link](./test/hard/test_239_sliding_window_maximum.rb) |
655655
| 297. Serialize and Deserialize Binary Tree | [Link](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Link](./lib/hard/297_serialize_and_deserialize_binary_tree.rb) | [Link](./test/hard/test_297_serialize_and_deserialize_binary_tree.rb) |
656656
| 336. Palindrome Pairs | [Link](https://leetcode.com/problems/palindrome-pairs/) | [Link](./lib/hard/336_palindrome_pairs.rb) | [Link](./test/hard/test_336_palindrome_pairs.rb) |
657+
| 363. Max Sum of Rectangle No Larger Than K | [Link](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [Link](./lib/hard/363_max_sum_of_rectangle_no_larger_than_k.rb) | [Link](./test/hard/test_363_max_sum_of_rectangle_no_larger_than_k.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.8.4'
8+
s.version = '7.8.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/
4+
# @param {Integer[][]} matrix
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def max_sum_submatrix(matrix, k)
8+
m = matrix.length
9+
n = matrix[0].length
10+
11+
result = -::Float::INFINITY
12+
13+
(0...n).each do |start_col|
14+
sum_row = ::Array.new(m, 0)
15+
16+
(start_col...n).each do |last_col|
17+
sum = 0
18+
19+
accum_sums = [0]
20+
(0...m).each do |last_row|
21+
sum += (sum_row[last_row] += matrix[last_row][last_col])
22+
23+
excess = accum_sums.bsearch { |x| x >= sum - k }
24+
25+
result = [result, sum - excess].max unless excess.nil?
26+
27+
idx = accum_sums.bsearch_index { |x| x >= sum }
28+
29+
accum_sums.insert(idx || (last_row + 1), sum)
30+
end
31+
end
32+
end
33+
34+
result
35+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/hard/363_max_sum_of_rectangle_no_larger_than_k'
5+
require 'minitest/autorun'
6+
7+
class MaxSumOfRectangleNoLargerThanKTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
2,
11+
max_sum_submatrix(
12+
[
13+
[1, 0, 1],
14+
[0, -2, 3]
15+
],
16+
2
17+
)
18+
)
19+
end
20+
21+
def test_default_two
22+
assert_equal(
23+
3,
24+
max_sum_submatrix(
25+
[
26+
[2, 2, -1]
27+
],
28+
3
29+
)
30+
)
31+
end
32+
end

0 commit comments

Comments
 (0)