Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create 0981-Time-Based-Key-Value-Store.rb#1294

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Ahmad-A0 merged 1 commit intoneetcode-gh:mainfromvoski:patch-9
Oct 21, 2022
Merged
Changes fromall 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
54 changes: 54 additions & 0 deletionsruby/0981-Time-Based-Key-Value-Store.rb
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
class TimeMap
def initialize()
@map = {}
end


=begin
:type key: String
:type value: String
:type timestamp: Integer
:rtype: Void
=end
def set(key, value, timestamp)
@map[key] = [] unless @map.key?(key)

@map[key] << { v: value, t: timestamp }
end


=begin
:type key: String
:type timestamp: Integer
:rtype: String
=end
def get(key, timestamp)
return "" unless @map.key?(key)

entries = @map[key]
return "" if timestamp < entries[0][:t]
return entries[-1][:v] if timestamp >= entries[-1][:t]

l = 0
r = entries.length - 1
max = entries[0]
while l <= r
mid = ( l + r ) / 2
curr = entries[mid]

if curr[:t] < timestamp
max = curr
l = mid + 1
else
r = mid - 1
end
end

max[:v]
end
end

# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap.new()
# obj.set(key, value, timestamp)
# param_2 = obj.get(key, timestamp)

[8]ページ先頭

©2009-2025 Movatter.jp