Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
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 = '7.4.2'
s.version = '7.4.3'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
53 changes: 53 additions & 0 deletions lib/medium/677_map_sum_pairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

# https://leetcode.com/problems/map-sum-pairs/
class MapSum
# Init
def initialize
@root = ::MapSum::Node.new
@values = {}
end

# @param {String} key
# @param {Integer} val
# @return {Void}
def insert(key, val)
delta = val - @values.fetch(key, 0)
@values[key] = val
curr = @root
curr.score += delta

(0...key.size).each do |i|
c = key[i]
curr.children[c] = ::MapSum::Node.new unless curr.children.key?(c)
curr = curr.children[c]
curr.score += delta
end
end

# @param {String} prefix
# @return {Integer}
def sum(prefix)
curr = @root

(0...prefix.size).each do |i|
c = prefix[i]
curr = curr.children[c]

return 0 unless curr
end

curr.score
end

# Node realization
class Node
attr_accessor :children, :score

# Init
def initialize
@children = {}
@score = 0
end
end
end
18 changes: 18 additions & 0 deletions test/medium/test_677_map_sum_pairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/677_map_sum_pairs'
require 'minitest/autorun'

class MapSumPairsTest < ::Minitest::Test
def test_default_one
map_sum = ::MapSum.new
map_sum.insert('apple', 3)

assert_equal(3, map_sum.sum('ap'))

map_sum.insert('app', 2)

assert_equal(5, map_sum.sum('ap'))
end
end
Loading