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

Commit d894a94

Browse files
30 days of javaScript 17th problem solved
1 parent e75342b commit d894a94

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
3+
Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key.
4+
5+
The class has three public methods:
6+
7+
set(key, value, duration): accepts an integer key, an integer value, and a duration in milliseconds. Once the duration has elapsed, the key should be inaccessible. The method should return true if the same un-expired key already exists and false otherwise. Both the value and duration should be overwritten if the key already exists.
8+
9+
get(key): if an un-expired key exists, it should return the associated value. Otherwise it should return -1.
10+
11+
count(): returns the count of un-expired keys.
12+
13+
14+
15+
Example 1:
16+
17+
Input:
18+
actions = ["TimeLimitedCache", "set", "get", "count", "get"]
19+
values = [[], [1, 42, 100], [1], [], [1]]
20+
timeDelays = [0, 0, 50, 50, 150]
21+
Output: [null, false, 42, 1, -1]
22+
Explanation:
23+
At t=0, the cache is constructed.
24+
At t=0, a key-value pair (1: 42) is added with a time limit of 100ms. The value doesn't exist so false is returned.
25+
At t=50, key=1 is requested and the value of 42 is returned.
26+
At t=50, count() is called and there is one active key in the cache.
27+
At t=100, key=1 expires.
28+
At t=150, get(1) is called but -1 is returned because the cache is empty.
29+
Example 2:
30+
31+
Input:
32+
actions = ["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]
33+
values = [[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]
34+
timeDelays = [0, 0, 40, 50, 120, 200, 250]
35+
Output: [null, false, true, 50, 50, -1, 0]
36+
Explanation:
37+
At t=0, the cache is constructed.
38+
At t=0, a key-value pair (1: 42) is added with a time limit of 50ms. The value doesn't exist so false is returned.
39+
At t=40, a key-value pair (1: 50) is added with a time limit of 100ms. A non-expired value already existed so true is returned and the old value was overwritten.
40+
At t=50, get(1) is called which returned 50.
41+
At t=120, get(1) is called which returned 50.
42+
At t=140, key=1 expires.
43+
At t=200, get(1) is called but the cache is empty so -1 is returned.
44+
At t=250, count() returns 0 because the cache is empty.
45+
46+
47+
Constraints:
48+
49+
0 <= key, value <= 109
50+
0 <= duration <= 1000
51+
1 <= actions.length <= 100
52+
actions.length === values.length
53+
actions.length === timeDelays.length
54+
0 <= timeDelays[i] <= 1450
55+
actions[i] is one of "TimeLimitedCache", "set", "get" and "count"
56+
First action is always "TimeLimitedCache" and must be executed immediately, with a 0-millisecond delay
57+
58+
59+
60+
61+
62+
63+
*/
64+
65+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /