Skip to content

2025-01-20 v. 8.1.8: added "981. Time Based Key-Value Store" #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
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 @@ -650,6 +650,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 967. Numbers With Same Consecutive Differences | [Link](https://leetcode.com/problems/numbers-with-same-consecutive-differences/) | [Link](./lib/medium/967_numbers_with_same_consecutive_differences.rb) | [Link](./test/medium/test_967_numbers_with_same_consecutive_differences.rb) |
| 973. K Closest Points to Origin | [Link](https://leetcode.com/problems/k-closest-points-to-origin/) | [Link](./lib/medium/973_k_closest_points_to_origin.rb) | [Link](./test/medium/test_973_k_closest_points_to_origin.rb) |
| 979. Distribute Coins in Binary Tree | [Link](https://leetcode.com/problems/distribute-coins-in-binary-tree/) | [Link](./lib/medium/979_distribute_coins_in_binary_tree.rb) | [Link](./test/medium/test_979_distribute_coins_in_binary_tree.rb) |
| 981. Time Based Key-Value Store | [Link](https://leetcode.com/problems/time-based-key-value-store/) | [Link](./lib/medium/981_time_based_key_value_store.rb) | [Link](./test/medium/test_981_time_based_key_value_store.rb) |
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
Expand Down
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 = '8.1.7'
s.version = '8.1.8'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
40 changes: 40 additions & 0 deletions lib/medium/981_time_based_key_value_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

# https://leetcode.com/problems/time-based-key-value-store/
class TimeMap
# Init
def initialize
@keys = {}
end

# @param {String} key
# @param {String} value
# @param {Integer} timestamp
# @return {Void}
def set(key, value, timestamp)
@keys[key] ||= []
@keys[key] << [timestamp, value]
end

# @param {String} key
# @param {Integer} timestamp
# @return {Void}
def get(key, timestamp)
return '' unless @keys.key?(key) && timestamp >= @keys[key].first[0]

l = 0
r = @keys[key].size

while l < r
m = (l + r) / 2

if @keys[key][m][0] <= timestamp
l = m + 1
else
r = m
end
end

r.zero? ? '' : @keys[key][r - 1][1]
end
end
34 changes: 34 additions & 0 deletions test/medium/test_981_time_based_key_value_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/981_time_based_key_value_store'
require 'minitest/autorun'

class TimeBasedKeyValueStoreTest < ::Minitest::Test
def test_default_one
time_map = ::TimeMap.new

time_map.set('foo', 'bar', 1)

assert_equal('bar', time_map.get('foo', 1))
assert_equal('bar', time_map.get('foo', 3))

time_map.set('foo', 'bar2', 4)

assert_equal('bar2', time_map.get('foo', 4))
assert_equal('bar2', time_map.get('foo', 5))
end

def test_additional_one
time_map = ::TimeMap.new

time_map.set('love', 'high', 10)
time_map.set('love', 'low', 20)

assert_equal('', time_map.get('love', 5))
assert_equal('high', time_map.get('love', 10))
assert_equal('high', time_map.get('love', 15))
assert_equal('low', time_map.get('love', 20))
assert_equal('low', time_map.get('love', 25))
end
end
Loading