Skip to content

Commit 67d727f

Browse files
authored
2025-01-20 v. 8.1.8: added "981. Time Based Key-Value Store"
2 parents 906095b + d5eb716 commit 67d727f

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
650650
| 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) |
651651
| 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) |
652652
| 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) |
653+
| 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) |
653654
| 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) |
654655
| 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) |
655656
| 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) |

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 = '8.1.7'
8+
s.version = '8.1.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/time-based-key-value-store/
4+
class TimeMap
5+
# Init
6+
def initialize
7+
@keys = {}
8+
end
9+
10+
# @param {String} key
11+
# @param {String} value
12+
# @param {Integer} timestamp
13+
# @return {Void}
14+
def set(key, value, timestamp)
15+
@keys[key] ||= []
16+
@keys[key] << [timestamp, value]
17+
end
18+
19+
# @param {String} key
20+
# @param {Integer} timestamp
21+
# @return {Void}
22+
def get(key, timestamp)
23+
return '' unless @keys.key?(key) && timestamp >= @keys[key].first[0]
24+
25+
l = 0
26+
r = @keys[key].size
27+
28+
while l < r
29+
m = (l + r) / 2
30+
31+
if @keys[key][m][0] <= timestamp
32+
l = m + 1
33+
else
34+
r = m
35+
end
36+
end
37+
38+
r.zero? ? '' : @keys[key][r - 1][1]
39+
end
40+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/981_time_based_key_value_store'
5+
require 'minitest/autorun'
6+
7+
class TimeBasedKeyValueStoreTest < ::Minitest::Test
8+
def test_default_one
9+
time_map = ::TimeMap.new
10+
11+
time_map.set('foo', 'bar', 1)
12+
13+
assert_equal('bar', time_map.get('foo', 1))
14+
assert_equal('bar', time_map.get('foo', 3))
15+
16+
time_map.set('foo', 'bar2', 4)
17+
18+
assert_equal('bar2', time_map.get('foo', 4))
19+
assert_equal('bar2', time_map.get('foo', 5))
20+
end
21+
22+
def test_additional_one
23+
time_map = ::TimeMap.new
24+
25+
time_map.set('love', 'high', 10)
26+
time_map.set('love', 'low', 20)
27+
28+
assert_equal('', time_map.get('love', 5))
29+
assert_equal('high', time_map.get('love', 10))
30+
assert_equal('high', time_map.get('love', 15))
31+
assert_equal('low', time_map.get('love', 20))
32+
assert_equal('low', time_map.get('love', 25))
33+
end
34+
end

0 commit comments

Comments
 (0)