Skip to content

Commit b2e8d1a

Browse files
authored
2024-12-20 v. 7.4.3: added "677. Map Sum Pairs"
2 parents 1197f06 + 3f862b6 commit b2e8d1a

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -607,3 +607,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
607607
| 659. Split Array into Consecutive Subsequences | [Link](https://leetcode.com/problems/split-array-into-consecutive-subsequences/) | [Link](./lib/medium/659_split_array_into_consecutive_subsequences.rb) | [Link](./test/medium/test_659_split_array_into_consecutive_subsequences.rb) |
608608
| 662. Maximum Width of Binary Tree | [Link](https://leetcode.com/problems/maximum-width-of-binary-tree/) | [Link](./lib/medium/662_maximum_width_of_binary_tree.rb) | [Link](./test/medium/test_662_maximum_width_of_binary_tree.rb) |
609609
| 669. Trim a Binary Search Tree | [Link](https://leetcode.com/problems/trim-a-binary-search-tree/) | [Link](./lib/medium/669_trim_a_binary_search_tree.rb) | [Link](./test/medium/test_669_trim_a_binary_search_tree.rb) |
610+
| 677. Map Sum Pairs | [Link](https://leetcode.com/problems/map-sum-pairs/) | [Link](./lib/medium/677_map_sum_pairs.rb) | [Link](./test/medium/test_677_map_sum_pairs.rb) |

leetcode-ruby.gemspec

+1-1
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.4.2'
8+
s.version = '7.4.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/677_map_sum_pairs.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/map-sum-pairs/
4+
class MapSum
5+
# Init
6+
def initialize
7+
@root = ::MapSum::Node.new
8+
@values = {}
9+
end
10+
11+
# @param {String} key
12+
# @param {Integer} val
13+
# @return {Void}
14+
def insert(key, val)
15+
delta = val - @values.fetch(key, 0)
16+
@values[key] = val
17+
curr = @root
18+
curr.score += delta
19+
20+
(0...key.size).each do |i|
21+
c = key[i]
22+
curr.children[c] = ::MapSum::Node.new unless curr.children.key?(c)
23+
curr = curr.children[c]
24+
curr.score += delta
25+
end
26+
end
27+
28+
# @param {String} prefix
29+
# @return {Integer}
30+
def sum(prefix)
31+
curr = @root
32+
33+
(0...prefix.size).each do |i|
34+
c = prefix[i]
35+
curr = curr.children[c]
36+
37+
return 0 unless curr
38+
end
39+
40+
curr.score
41+
end
42+
43+
# Node realization
44+
class Node
45+
attr_accessor :children, :score
46+
47+
# Init
48+
def initialize
49+
@children = {}
50+
@score = 0
51+
end
52+
end
53+
end

test/medium/test_677_map_sum_pairs.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/677_map_sum_pairs'
5+
require 'minitest/autorun'
6+
7+
class MapSumPairsTest < ::Minitest::Test
8+
def test_default_one
9+
map_sum = ::MapSum.new
10+
map_sum.insert('apple', 3)
11+
12+
assert_equal(3, map_sum.sum('ap'))
13+
14+
map_sum.insert('app', 2)
15+
16+
assert_equal(5, map_sum.sum('ap'))
17+
end
18+
end

0 commit comments

Comments
 (0)