Skip to content

Commit d964054

Browse files
authored
Merge pull request #750 from fartem/304_Range_Sum_Query_2D-Immutable
2024-10-17 v. 6.8.3: added "304. Range Sum Query 2D - Immutable"
2 parents 3e8f436 + 693acf0 commit d964054

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
548548
| 287. Find the Duplicate Number | [Link](https://leetcode.com/problems/find-the-duplicate-number/) | [Link](./lib/medium/287_find_the_duplicate_number.rb) | [Link](./test/medium/test_287_find_the_duplicate_number.rb) |
549549
| 299. Bulls and Cows | [Link](https://leetcode.com/problems/bulls-and-cows/) | [Link](./lib/medium/299_bulls_and_cows.rb) | [Link](./test/medium/test_299_bulls_and_cows.rb) |
550550
| 300. Longest Increasing Subsequence | [Link](https://leetcode.com/problems/longest-increasing-subsequence/) | [Link](./lib/medium/300_longest_increasing_subsequence.rb) | [Link](./test/medium/test_300_longest_increasing_subsequence.rb) |
551+
| 304. Range Sum Query 2D - Immutable | [Link](https://leetcode.com/problems/range-sum-query-2d-immutable/) | [Link](./lib/medium/304_range_sum_query_2d_immutable.rb) | [Link](./test/medium/test_304_range_sum_query_2d_immutable.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 = '6.8.2'
8+
s.version = '6.8.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/range-sum-query-2d-immutable/
4+
class NumMatrix
5+
# @param {Integer[][]}
6+
def initialize(matrix)
7+
return if matrix.empty? || matrix.first.empty?
8+
9+
@dp = ::Array.new(matrix.size + 1) { ::Array.new(matrix[0].size + 1, 0) }
10+
matrix.each_with_index do |row, r|
11+
row.each_with_index do |val, c|
12+
@dp[r + 1][c + 1] = @dp[r + 1][c] + @dp[r][c + 1] + val - @dp[r][c]
13+
end
14+
end
15+
end
16+
17+
# @param {Integer} row1
18+
# @param {Integer} col1
19+
# @param {Integer} row2
20+
# @param {Integer} col2
21+
# @return {Integer}
22+
def sum_region(row1, col1, row2, col2)
23+
@dp[row2 + 1][col2 + 1] - @dp[row1][col2 + 1] - @dp[row2 + 1][col1] + @dp[row1][col1]
24+
end
25+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/304_range_sum_query_2d_immutable'
5+
require 'minitest/autorun'
6+
7+
class RangeSumQuery2DImmutableTest < ::Minitest::Test
8+
def test_default_one
9+
num_matrix = ::NumMatrix.new(
10+
[
11+
[3, 0, 1, 4, 2],
12+
[5, 6, 3, 2, 1],
13+
[1, 2, 0, 1, 5],
14+
[4, 1, 0, 1, 7],
15+
[1, 0, 3, 0, 5]
16+
]
17+
)
18+
19+
assert_equal(8, num_matrix.sum_region(2, 1, 4, 3))
20+
assert_equal(11, num_matrix.sum_region(1, 1, 2, 2))
21+
assert_equal(12, num_matrix.sum_region(1, 2, 2, 4))
22+
end
23+
end

0 commit comments

Comments
 (0)