LeetCode 题解工作台

有时间限制的缓存

编写一个类,它允许获取和设置键-值对,并且每个键都有一个 过期时间 。 该类有三个公共方法: set(key, value, duration) :接收参数为整型键 key 、整型值 value 和以毫秒为单位的持续时间 duration 。一旦 duration 到期后,这个键就无法访问。如果相同…

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

中等 · cache·time·limit·core·interview·pattern

bolt

答案摘要

我们用哈希表 记录键值对,其中键为整型键 ,值为一个数组,数组的第一个元素为整型值 ,第二个元素为元素的过期时间 。 我们实现一个 `removeExpire` 方法,用于删除过期的键值对。在 `set`、`get` 和 `count` 方法中,我们先调用 `removeExpire` 方法,然后再进行相应的操作。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 cache·time·limit·core·interview·pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

编写一个类,它允许获取和设置键-值对,并且每个键都有一个 过期时间 。

该类有三个公共方法:

set(key, value, duration) :接收参数为整型键 key 、整型值 value 和以毫秒为单位的持续时间 duration 。一旦 duration 到期后,这个键就无法访问。如果相同的未过期键已经存在,该方法将返回 true ,否则返回 false 。如果该键已经存在,则它的值和持续时间都应该被覆盖。

get(key) :如果存在一个未过期的键,它应该返回这个键相关的值。否则返回 -1 。

count() :返回未过期键的总数。

 

示例 1:

输入: 
actions = ["TimeLimitedCache", "set", "get", "count", "get"]
values = [[], [1, 42, 100], [1], [], [1]]
timeDelays = [0, 0, 50, 50, 150]
输出: [null, false, 42, 1, -1]
解释:
在 t=0 时,缓存被构造。
在 t=0 时,添加一个键值对 (1: 42) ,过期时间为 100ms 。因为该值不存在,因此返回false。
在 t=50 时,请求 key=1 并返回值 42。
在 t=50 时,调用 count() ,缓存中有一个未过期的键。
在 t=100 时,key=1 到期。
在 t=150 时,调用 get(1) ,返回 -1,因为缓存是空的。

示例 2:

输入:
actions = ["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]
values = [[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]
timeDelays = [0, 0, 40, 50, 120, 200, 250]
输出: [null, false, true, 50, 50, -1]
解释:
在 t=0 时,缓存被构造。
在 t=0 时,添加一个键值对 (1: 42) ,过期时间为 50ms。因为该值不存在,因此返回false。
当 t=40 时,添加一个键值对 (1: 50) ,过期时间为 100ms。因为一个未过期的键已经存在,返回 true 并覆盖这个键的旧值。
在 t=50 时,调用 get(1) ,返回 50。
在 t=120 时,调用 get(1) ,返回 50。
在 t=140 时,key=1 过期。
在 t=200 时,调用 get(1) ,但缓存为空,因此返回 -1。
在 t=250 时,count() 返回0 ,因为缓存是空的,没有未过期的键。

 

提示:

  • 0 <= key, value <= 109
  • 0 <= duration <= 1000
  • 1 <= actions.length <= 100
  • actions.length === values.length
  • actions.length === timeDelays.length
  • 0 <= timeDelays[i] <= 1450
  • actions[i] 是 "TimeLimitedCache"、"set"、"get" 和 "count" 中的一个。
  • 第一个操作始终是 "TimeLimitedCache" 而且一定会以 0 毫秒的延迟立即执行
lightbulb

解题思路

方法一:哈希表

我们用哈希表 cachecache 记录键值对,其中键为整型键 keykey,值为一个数组,数组的第一个元素为整型值 valuevalue,第二个元素为元素的过期时间 expireexpire

我们实现一个 removeExpire 方法,用于删除过期的键值对。在 setgetcount 方法中,我们先调用 removeExpire 方法,然后再进行相应的操作。

时间复杂度为 O(1)O(1),空间复杂度为 O(n)O(n)。其中 nn 为哈希表 cachecache 的大小。

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
class TimeLimitedCache {
    #cache: Map<number, [value: number, expire: number]> = new Map();

    set(key: number, value: number, duration: number): boolean {
        const isExist = this.#cache.has(key);

        if (!this.#isExpired(key)) {
            this.#cache.set(key, [value, Date.now() + duration]);
        }

        return isExist;
    }

    get(key: number): number {
        if (this.#isExpired(key)) return -1;
        const res = this.#cache.get(key)?.[0] ?? -1;
        return res;
    }

    count(): number {
        const xs = Array.from(this.#cache).filter(([key]) => !this.#isExpired(key));
        return xs.length;
    }

    #isExpired = (key: number) =>
        this.#cache.has(key) &&
        (this.#cache.get(key)?.[1] ?? Number.NEGATIVE_INFINITY) < Date.now();
}

/**
 * Your TimeLimitedCache object will be instantiated and called as such:
 * var obj = new TimeLimitedCache()
 * obj.set(1, 42, 1000); // false
 * obj.get(1) // 42
 * obj.count() // 1
 */
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Can the candidate efficiently handle key expiration using timers?

  • question_mark

    Is the candidate able to optimize for both space and time complexity while managing cache expiration?

  • question_mark

    Does the candidate ensure that overwriting a key correctly updates both the value and the expiration timer?

warning

常见陷阱

外企场景
  • error

    Not handling key overwrites properly and not resetting the expiration timer.

  • error

    Failing to clean up expired keys efficiently, leading to memory leaks or slow performance.

  • error

    Not accounting for the precise timing of expiration and the effect of delayed get or count operations.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Implementing a persistent cache where keys survive beyond program execution.

  • arrow_right_alt

    Handling different expiration durations for each key dynamically in more complex use cases.

  • arrow_right_alt

    Optimizing for large-scale caches where multiple operations need to be processed concurrently.

help

常见问题

外企场景

有时间限制的缓存题解:cache·time·limit·core·i… | LeetCode #2622 中等