#2336
Medium
auto_awesome

LeetCode 题解工作台

无限集中的最小数字

现有一个包含所有正整数的集合 [1, 2, 3, 4, 5, ...] 。 实现 SmallestInfiniteSet 类: SmallestInfiniteSet() 初始化 SmallestInfiniteSet 对象以包含 所有 正整数。 int popSmallest() 移除 并返回该无…

category

4

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 ·

bolt

答案摘要

我们注意到,题目中集合的元素范围是 $[1, 1000]$,并且我们需要支持的操作有: - `popSmallest`:弹出集合中的最小元素

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 堆 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

现有一个包含所有正整数的集合 [1, 2, 3, 4, 5, ...]

实现 SmallestInfiniteSet 类:

  • SmallestInfiniteSet() 初始化 SmallestInfiniteSet 对象以包含 所有 正整数。
  • int popSmallest() 移除 并返回该无限集中的最小整数。
  • void addBack(int num) 如果正整数 num 存在于无限集中,则将一个 num 添加 到该无限集中。

 

示例:

输入
["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
[[], [2], [], [], [], [1], [], [], []]
输出
[null, null, 1, 2, 3, null, 1, 4, 5]

解释
SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
smallestInfiniteSet.addBack(2);    // 2 已经在集合中,所以不做任何变更。
smallestInfiniteSet.popSmallest(); // 返回 1 ,因为 1 是最小的整数,并将其从集合中移除。
smallestInfiniteSet.popSmallest(); // 返回 2 ,并将其从集合中移除。
smallestInfiniteSet.popSmallest(); // 返回 3 ,并将其从集合中移除。
smallestInfiniteSet.addBack(1);    // 将 1 添加到该集合中。
smallestInfiniteSet.popSmallest(); // 返回 1 ,因为 1 在上一步中被添加到集合中,
                                   // 且 1 是最小的整数,并将其从集合中移除。
smallestInfiniteSet.popSmallest(); // 返回 4 ,并将其从集合中移除。
smallestInfiniteSet.popSmallest(); // 返回 5 ,并将其从集合中移除。

 

提示:

  • 1 <= num <= 1000
  • 最多调用 popSmallestaddBack 方法 共计 1000
lightbulb

解题思路

方法一:有序集合 + 模拟

我们注意到,题目中集合的元素范围是 [1,1000][1, 1000],并且我们需要支持的操作有:

  • popSmallest:弹出集合中的最小元素
  • addBack:向集合中添加元素

因此,我们可以使用有序集合来模拟,不妨记有序集合为 ss,集合中的元素为 s1,s2,,sns_1, s_2, \cdots, s_n,其中 nn 为有序集合中的元素个数。本题中 n1000n \le 1000

我们在初始化时,将 [1,1000][1, 1000] 中的所有元素加入有序集合中。时间复杂度 O(n×logn)O(n \times \log n)

popSmallest 操作中,我们只需要弹出有序集合中的第一个元素即可。单次操作时间复杂度 O(logn)O(\log n)

addBack 操作中,我们只需要将元素加入有序集合中即可。单次操作时间复杂度 O(logn)O(\log n)

空间复杂度 O(n)O(n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class SmallestInfiniteSet:
    def __init__(self):
        self.s = SortedSet(range(1, 1001))

    def popSmallest(self) -> int:
        x = self.s[0]
        self.s.remove(x)
        return x

    def addBack(self, num: int) -> None:
        self.s.add(num)


# Your SmallestInfiniteSet object will be instantiated and called as such:
# obj = SmallestInfiniteSet()
# param_1 = obj.popSmallest()
# obj.addBack(num)
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Candidates may struggle with balancing efficient popSmallest and addBack operations.

  • question_mark

    Look for solutions using a combination of data structures, especially heaps and hash tables.

  • question_mark

    Ensure the candidate understands the trade-off between space complexity and operation speed.

warning

常见陷阱

外企场景
  • error

    Overusing brute force methods for tracking elements will lead to inefficiency.

  • error

    Not handling the edge case where a number is popped and then added back multiple times.

  • error

    Failing to optimize for time complexity in popSmallest or addBack, leading to suboptimal performance.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Handling a larger range of numbers efficiently, where the set grows even more.

  • arrow_right_alt

    Adding additional operations, such as checking if a specific number exists in the set.

  • arrow_right_alt

    Optimizing for worst-case time complexity when the number of operations increases.

help

常见问题

外企场景

无限集中的最小数字题解:堆 | LeetCode #2336 中等