|
| 1 | +// Question Link: https://leetcode.com/problems/cache-with-time-limit/?envType=study-plan-v2&envId=30-days-of-javascript |
| 2 | +// Solution Link: |
| 3 | + |
| 4 | +/* |
| 5 | +2622. Cache With Time Limit |
| 6 | +
|
| 7 | +Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key. |
| 8 | +
|
| 9 | +The class has three public methods: |
| 10 | +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. |
| 11 | +get(key): if an un-expired key exists, it should return the associated value. Otherwise it should return -1. |
| 12 | +count(): returns the count of un-expired keys. |
| 13 | +
|
| 14 | +Example 1: |
| 15 | +Input: |
| 16 | +actions = ["TimeLimitedCache", "set", "get", "count", "get"] |
| 17 | +values = [[], [1, 42, 100], [1], [], [1]] |
| 18 | +timeDelays = [0, 0, 50, 50, 150] |
| 19 | +Output: [null, false, 42, 1, -1] |
| 20 | +Explanation: |
| 21 | +At t=0, the cache is constructed. |
| 22 | +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. |
| 23 | +At t=50, key=1 is requested and the value of 42 is returned. |
| 24 | +At t=50, count() is called and there is one active key in the cache. |
| 25 | +At t=100, key=1 expires. |
| 26 | +At t=150, get(1) is called but -1 is returned because the cache is empty. |
| 27 | +
|
| 28 | +Example 2: |
| 29 | +Input: |
| 30 | +actions = ["TimeLimitedCache", "set", "set", "get", "get", "get", "count"] |
| 31 | +values = [[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []] |
| 32 | +timeDelays = [0, 0, 40, 50, 120, 200, 250] |
| 33 | +Output: [null, false, true, 50, 50, -1, 0] |
| 34 | +Explanation: |
| 35 | +At t=0, the cache is constructed. |
| 36 | +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. |
| 37 | +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. |
| 38 | +At t=50, get(1) is called which returned 50. |
| 39 | +At t=120, get(1) is called which returned 50. |
| 40 | +At t=140, key=1 expires. |
| 41 | +At t=200, get(1) is called but the cache is empty so -1 is returned. |
| 42 | +At t=250, count() returns 0 because the cache is empty. |
| 43 | + |
| 44 | +Constraints: |
| 45 | +0 <= key, value <= 109 |
| 46 | +0 <= duration <= 1000 |
| 47 | +1 <= actions.length <= 100 |
| 48 | +actions.length === values.length |
| 49 | +actions.length === timeDelays.length |
| 50 | +0 <= timeDelays[i] <= 1450 |
| 51 | +actions[i] is one of "TimeLimitedCache", "set", "get" and "count" |
| 52 | +First action is always "TimeLimitedCache" and must be executed immediately, with a 0-millisecond delay |
| 53 | +*/ |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +var TimeLimitedCache = function () { |
| 58 | + this.data = {}; |
| 59 | + this.active = 0; |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * @param {number} key |
| 64 | + * @param {number} value |
| 65 | + * @param {number} duration time until expiration in ms |
| 66 | + * @return {boolean} if un-expired key already existed |
| 67 | + */ |
| 68 | +TimeLimitedCache.prototype.set = function (key, value, duration) { |
| 69 | + const prevActive = !!this.data[key]?.isActive; |
| 70 | + prevActive ? clearTimeout(this.data[key].timeoutId) : this.active++; |
| 71 | + |
| 72 | + const timeoutId = setTimeout(() => { |
| 73 | + this.active--; |
| 74 | + this.data[key].isActive = false; |
| 75 | + }, duration); |
| 76 | + |
| 77 | + this.data[key] = { |
| 78 | + isActive: true, |
| 79 | + value, |
| 80 | + timeoutId |
| 81 | + } |
| 82 | + |
| 83 | + return prevActive; |
| 84 | +}; |
| 85 | + |
| 86 | +/** |
| 87 | + * @param {number} key |
| 88 | + * @return {number} value associated with key |
| 89 | + */ |
| 90 | +TimeLimitedCache.prototype.get = function (key) { |
| 91 | + return this.data[key]?.isActive ? this.data[key].value : -1; |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * @return {number} count of non-expired keys |
| 96 | + */ |
| 97 | +TimeLimitedCache.prototype.count = function () { |
| 98 | + return this.active; |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * const timeLimitedCache = new TimeLimitedCache() |
| 103 | + * timeLimitedCache.set(1, 42, 1000); // false |
| 104 | + * timeLimitedCache.get(1) // 42 |
| 105 | + * timeLimitedCache.count() // 1 |
| 106 | + */ |
0 commit comments