2336. Smallest Number in Infinite Set Medium
1/**
2 * [2336] Smallest Number in Infinite Set
3 *
4 * You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].
5 * Implement the SmallestInfiniteSet class:
6 *
7 * SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers.
8 * int popSmallest() Removes and returns the smallest integer contained in the infinite set.
9 * void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set.
10 *
11 *
12 * Example 1:
13 *
14 * Input
15 * ["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
16 * [[], [2], [], [], [], [1], [], [], []]
17 * Output
18 * [null, null, 1, 2, 3, null, 1, 4, 5]
19 * Explanation
20 * SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
21 * smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change is made.
22 * smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.
23 * smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
24 * smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
25 * smallestInfiniteSet.addBack(1); // 1 is added back to the set.
26 * smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and
27 * // is the smallest number, and remove it from the set.
28 * smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
29 * smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
30 *
31 *
32 * Constraints:
33 *
34 * 1 <= num <= 1000
35 * At most 1000 calls will be made in total to popSmallest and addBack.
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/smallest-number-in-infinite-set/
41// discuss: https://leetcode.com/problems/smallest-number-in-infinite-set/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45struct SmallestInfiniteSet {
46 false
47 }
48
49
50/**
51 * `&self` means the method takes an immutable reference.
52 * If you need a mutable reference, change it to `&mut self` instead.
53 */
54impl SmallestInfiniteSet {
55
56 fn new() -> Self {
57
58 }
59
60 fn pop_smallest(&self) -> i32 {
61
62 }
63
64 fn add_back(&self, num: i32) {
65
66 }
67}
68
69/**
70 * Your SmallestInfiniteSet object will be instantiated and called as such:
71 * let obj = SmallestInfiniteSet::new();
72 * let ret_1: i32 = obj.pop_smallest();
73 * obj.add_back(num);
74 */
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn test_2336() {
84 }
85}
86
Back
© 2025 bowen.ge All Rights Reserved.