leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 78b7489d614d3b618b75857bd22a123c284d331d
parent e0289b589d9be7030c3087e60182754341629590
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 18 May 2023 23:44:33 +0200

JavaScript Challenge: Day 14

Diffstat:
AProblems/2622.js | 21+++++++++++++++++++++
MREADME.md | 1+
2 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/Problems/2622.js b/Problems/2622.js @@ -0,0 +1,21 @@ +class TimeLimitedCache { + cache = new Map(); + + set(key, value, duration) { + const valueInCache = this.cache.get(key); + if (valueInCache) { + clearTimeout(valueInCache.timeout); + } + const timeout = setTimeout(() => this.cache.delete(key), duration); + this.cache.set(key, { value, timeout }); + return Boolean(valueInCache); + } + + get(key) { + return this.cache.has(key) ? this.cache.get(key).value : -1; + } + + count() { + return this.cache.size; + } +}; diff --git a/README.md b/README.md @@ -542,6 +542,7 @@ for solving problems. | 2497 | Medium | [Maximum Star Sum of a Graph](Problems/2497.cpp) | | 2620 | Easy | [Counter](Problems/2620.js) | | 2621 | Easy | [Sleep](Problems/2621.js) | +| 2622 | Medium | [Cache With Time Limit](Problems/2622.js) | | 2623 | Medium | [Memoize](Problems/2623.js) | | 2626 | Easy | [Array Reduce Transformation](Problems/2626.js) | | 2629 | Easy | [Function Composition](Problems/2629.js) |