LeetCode //C - 2336. Smallest Number in Infinite Set

2336. Smallest Number in Infinite Set

You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].

Implement the SmallestInfiniteSet class:

  • SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers.
  • int popSmallest() Removes and returns the smallest integer contained in the infinite set.
  • void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set.
Example 1:

Input:

["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]

[[], [2], [], [], [], [1], [], [], []]
Output:

[null, null, 1, 2, 3, null, 1, 4, 5]
Explanation

SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();

smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change is made.

smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.

smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.

smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.

smallestInfiniteSet.addBack(1); // 1 is added back to the set.

smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and

// is the smallest number, and remove it from the set.

smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.

smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.

Constraints:
  • 1 <= num <= 1000
  • At most 1000 calls will be made in total to popSmallest and addBack.

From: LeetCode

Link: 2336. Smallest Number in Infinite Set


Solution:

Ideas:

To implement the SmallestInfiniteSet in C, we need to maintain a data structure that allows us to efficiently find and remove the smallest number, as well as add numbers back to the set. A good choice for this task is a min-heap. However, since the numbers are from 1 to 1000 and we need to check if a number is already in the set, we can optimize this by using an array of booleans to keep track of which numbers are currently in the set.

The structure can have an integer to track the current smallest number and a boolean array to check if a number is in the set or has been removed. When popping the smallest number, we update the current smallest number to the next number that is present in the set. When adding a number back, we just update the boolean array.

Code:
c 复制代码
#define MAX_NUM 1000

typedef struct {
    int nextSmallest;
    bool present[MAX_NUM + 1];  // to track whether a number is in the set
} SmallestInfiniteSet;

SmallestInfiniteSet* smallestInfiniteSetCreate() {
    SmallestInfiniteSet* obj = malloc(sizeof(SmallestInfiniteSet));
    obj->nextSmallest = 1;
    for (int i = 1; i <= MAX_NUM; ++i) {
        obj->present[i] = true;
    }
    return obj;
}

int smallestInfiniteSetPopSmallest(SmallestInfiniteSet* obj) {
    int smallest = obj->nextSmallest;
    obj->present[smallest] = false;

    // Find the next smallest number
    while (obj->nextSmallest <= MAX_NUM && !obj->present[obj->nextSmallest]) {
        obj->nextSmallest++;
    }

    return smallest;
}

void smallestInfiniteSetAddBack(SmallestInfiniteSet* obj, int num) {
    if (!obj->present[num]) {
        obj->present[num] = true;
        if (num < obj->nextSmallest) {
            obj->nextSmallest = num;
        }
    }
}

void smallestInfiniteSetFree(SmallestInfiniteSet* obj) {
    free(obj);
}

/**
 * Your SmallestInfiniteSet struct will be instantiated and called as such:
 * SmallestInfiniteSet* obj = smallestInfiniteSetCreate();
 * int param_1 = smallestInfiniteSetPopSmallest(obj);
 * smallestInfiniteSetAddBack(obj, num);
 * smallestInfiniteSetFree(obj);
 */
相关推荐
好奇龙猫2 小时前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】
人工智能·算法
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01
人工智能·深度学习·神经网络·算法·机器学习·语言模型·数据挖掘
香菜大丸3 小时前
链表的归并排序
数据结构·算法·链表
jrrz08283 小时前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
oliveira-time3 小时前
golang学习2
算法
南宫生4 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步4 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝
DARLING Zero two♡4 小时前
关于我、重生到500年前凭借C语言改变世界科技vlog.16——万字详解指针概念及技巧
c语言·开发语言·科技
Ni-Guvara5 小时前
函数对象笔记
c++·算法
泉崎5 小时前
11.7比赛总结
数据结构·算法