FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/TimeBasedKeyValueStore.java at master · Orio77/LeetCode · GitHub
Orio77
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
TimeBasedKeyValueStore.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
59 lines (54 loc) · 1.77 KB
Breadcrumbs
LeetCode
/
TimeBasedKeyValueStore.java
Copy path
File metadata and controls
59 lines (54 loc) · 1.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import
java
.
util
.
Comparator
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Iterator
;
import
java
.
util
.
Map
;
import
java
.
util
.
PriorityQueue
;
class
TimeMap
{
private
Map
<
String
,
String
>
map
;
private
Map
<
String
,
PriorityQueue
<
Integer
>>
timestamps
;
public
TimeMap
() {
map
=
new
HashMap
<>();
timestamps
=
new
HashMap
<>();
}
public
void
set
(
String
key
,
String
value
,
int
timestamp
) {
map
.
put
(
key
+
"at timestamp: "
+
timestamp
,
value
);
if
(!
timestamps
.
containsKey
(
key
)) {
timestamps
.
put
(
key
,
new
PriorityQueue
<>(
Comparator
.
reverseOrder
()));
timestamps
.
get
(
key
).
add
(
timestamp
);
}
else
if
(
timestamps
.
containsKey
(
key
)) {
timestamps
.
get
(
key
).
add
(
timestamp
);
}
}
public
String
get
(
String
key
,
int
timestamp
) {
String
actualKey
=
key
+
"at timestamp: "
+
timestamp
;
if
(
map
.
containsKey
(
actualKey
)) {
return
map
.
get
(
actualKey
);
}
else
{
if
(
timestamps
.
get
(
key
) ==
null
) {
return
new
String
();
}
Iterator
<
Integer
>
iterator
=
timestamps
.
get
(
key
).
iterator
();
int
next
= -
1
;
int
curr
= -
1
;
while
(
iterator
.
hasNext
()) {
curr
=
iterator
.
next
();
if
(
curr
<
timestamp
) {
next
=
curr
;
return
map
.
get
(
key
+
"at timestamp: "
+
next
);
}
}
if
(
next
== -
1
) {
return
new
String
();
}
return
map
.
get
(
key
+
"at timestamp: "
+
next
);
}
}
}
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap obj = new TimeMap();
* obj.set(key,value,timestamp);
* String param_2 = obj.get(key,timestamp);
*/
Back
|
FazBrowse Home
|
New Git URL