Skip to content

Commit 7ddb65a

Browse files
committedFeb 8, 2025
Add solution #2336
1 parent e9fbc43 commit 7ddb65a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@
512512
2215|[Find the Difference of Two Arrays](./2215-find-the-difference-of-two-arrays.js)|Easy|
513513
2235|[Add Two Integers](./2235-add-two-integers.js)|Easy|
514514
2244|[Minimum Rounds to Complete All Tasks](./2244-minimum-rounds-to-complete-all-tasks.js)|Medium|
515+
2336|[Smallest Number in Infinite Set](./2336-smallest-number-in-infinite-set.js)|Medium|
515516
2352|[Equal Row and Column Pairs](./2352-equal-row-and-column-pairs.js)|Medium|
516517
2390|[Removing Stars From a String](./2390-removing-stars-from-a-string.js)|Medium|
517518
2396|[Strictly Palindromic Number](./2396-strictly-palindromic-number.js)|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 2336. Smallest Number in Infinite Set
3+
* https://leetcode.com/problems/smallest-number-in-infinite-set/
4+
* Difficulty: Medium
5+
*
6+
* You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].
7+
*
8+
* Implement the SmallestInfiniteSet class:
9+
* - SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all
10+
* positive integers.
11+
* - int popSmallest() Removes and returns the smallest integer contained in the
12+
* infinite set.
13+
* - void addBack(int num) Adds a positive integer num back into the infinite set,
14+
* if it is not already in the infinite set.
15+
*/
16+
17+
var SmallestInfiniteSet = function() {
18+
this.set = new Array(1000).fill(1);
19+
};
20+
21+
/**
22+
* @return {number}
23+
*/
24+
SmallestInfiniteSet.prototype.popSmallest = function() {
25+
const num = this.set.findIndex(n => n);
26+
this.set[num] = 0;
27+
return num + 1;
28+
};
29+
30+
/**
31+
* @param {number} num
32+
* @return {void}
33+
*/
34+
SmallestInfiniteSet.prototype.addBack = function(num) {
35+
this.set[num - 1] = 1;
36+
};

0 commit comments

Comments
 (0)
Please sign in to comment.