Skip to content

Commit

Permalink
Merge pull request #750 from fartem/304_Range_Sum_Query_2D-Immutable
Browse files Browse the repository at this point in the history
2024-10-17 v. 6.8.3: added "304. Range Sum Query 2D - Immutable"
  • Loading branch information
fartem authored Oct 17, 2024
2 parents 3e8f436 + 693acf0 commit d964054
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '6.8.2'
s.version = '6.8.3'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
25 changes: 25 additions & 0 deletions lib/medium/304_range_sum_query_2d_immutable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# https://leetcode.com/problems/range-sum-query-2d-immutable/
class NumMatrix
# @param {Integer[][]}
def initialize(matrix)
return if matrix.empty? || matrix.first.empty?

@dp = ::Array.new(matrix.size + 1) { ::Array.new(matrix[0].size + 1, 0) }
matrix.each_with_index do |row, r|
row.each_with_index do |val, c|
@dp[r + 1][c + 1] = @dp[r + 1][c] + @dp[r][c + 1] + val - @dp[r][c]
end
end
end

# @param {Integer} row1
# @param {Integer} col1
# @param {Integer} row2
# @param {Integer} col2
# @return {Integer}
def sum_region(row1, col1, row2, col2)
@dp[row2 + 1][col2 + 1] - @dp[row1][col2 + 1] - @dp[row2 + 1][col1] + @dp[row1][col1]
end
end
23 changes: 23 additions & 0 deletions test/medium/test_304_range_sum_query_2d_immutable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/304_range_sum_query_2d_immutable'
require 'minitest/autorun'

class RangeSumQuery2DImmutableTest < ::Minitest::Test
def test_default_one
num_matrix = ::NumMatrix.new(
[
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]
)

assert_equal(8, num_matrix.sum_region(2, 1, 4, 3))
assert_equal(11, num_matrix.sum_region(1, 1, 2, 2))
assert_equal(12, num_matrix.sum_region(1, 2, 2, 4))
end
end

0 comments on commit d964054

Please sign in to comment.