Skip to content

Commit 2c73a08

Browse files
authored
feat: add solutions to lc problem: No.2336 (doocs#2009)
No.2336.Smallest Number in Infinite Set
1 parent b243a20 commit 2c73a08

File tree

8 files changed

+1549
-461
lines changed

8 files changed

+1549
-461
lines changed

solution/2300-2399/2336.Smallest Number in Infinite Set/README.md

+722-181
Large diffs are not rendered by default.

solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md

+723-178
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
class SmallestInfiniteSet {
2-
public:
3-
unordered_set<int> black;
4-
5-
SmallestInfiniteSet() {
6-
}
7-
8-
int popSmallest() {
9-
int i = 1;
10-
for (; black.count(i); ++i)
11-
;
12-
black.insert(i);
13-
return i;
14-
}
15-
16-
void addBack(int num) {
17-
black.erase(num);
18-
}
19-
};
20-
21-
/**
22-
* Your SmallestInfiniteSet object will be instantiated and called as such:
23-
* SmallestInfiniteSet* obj = new SmallestInfiniteSet();
24-
* int param_1 = obj->popSmallest();
25-
* obj->addBack(num);
1+
class SmallestInfiniteSet {
2+
public:
3+
SmallestInfiniteSet() {
4+
for (int i = 1; i <= 1000; ++i) {
5+
s.insert(i);
6+
}
7+
}
8+
9+
int popSmallest() {
10+
int x = *s.begin();
11+
s.erase(s.begin());
12+
return x;
13+
}
14+
15+
void addBack(int num) {
16+
s.insert(num);
17+
}
18+
19+
private:
20+
set<int> s;
21+
};
22+
23+
/**
24+
* Your SmallestInfiniteSet object will be instantiated and called as such:
25+
* SmallestInfiniteSet* obj = new SmallestInfiniteSet();
26+
* int param_1 = obj->popSmallest();
27+
* obj->addBack(num);
2628
*/

solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
type SmallestInfiniteSet struct {
2-
black map[int]bool
2+
s *treemap.Map
33
}
44

55
func Constructor() SmallestInfiniteSet {
6-
s := map[int]bool{}
6+
s := treemap.NewWithIntComparator()
7+
for i := 1; i <= 1000; i++ {
8+
s.Put(i, nil)
9+
}
710
return SmallestInfiniteSet{s}
811
}
912

1013
func (this *SmallestInfiniteSet) PopSmallest() int {
11-
i := 1
12-
for ; this.black[i]; i++ {
13-
}
14-
this.black[i] = true
15-
return i
14+
x, _ := this.s.Min()
15+
this.s.Remove(x.(int))
16+
return x.(int)
1617
}
1718

1819
func (this *SmallestInfiniteSet) AddBack(num int) {
19-
this.black[num] = false
20+
this.s.Put(num, nil)
2021
}
2122

2223
/**
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
class SmallestInfiniteSet {
2-
private Set<Integer> black = new HashSet<>();
3-
4-
public SmallestInfiniteSet() {
5-
}
6-
7-
public int popSmallest() {
8-
int i = 1;
9-
for (; black.contains(i); ++i)
10-
;
11-
black.add(i);
12-
return i;
13-
}
14-
15-
public void addBack(int num) {
16-
black.remove(num);
17-
}
18-
}
19-
20-
/**
21-
* Your SmallestInfiniteSet object will be instantiated and called as such:
22-
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
23-
* int param_1 = obj.popSmallest();
24-
* obj.addBack(num);
1+
class SmallestInfiniteSet {
2+
private TreeSet<Integer> s = new TreeSet<>();
3+
4+
public SmallestInfiniteSet() {
5+
for (int i = 1; i <= 1000; ++i) {
6+
s.add(i);
7+
}
8+
}
9+
10+
public int popSmallest() {
11+
return s.pollFirst();
12+
}
13+
14+
public void addBack(int num) {
15+
s.add(num);
16+
}
17+
}
18+
19+
/**
20+
* Your SmallestInfiniteSet object will be instantiated and called as such:
21+
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
22+
* int param_1 = obj.popSmallest();
23+
* obj.addBack(num);
2524
*/
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
class SmallestInfiniteSet:
2-
def __init__(self):
3-
self.black = set()
4-
5-
def popSmallest(self) -> int:
6-
i = 1
7-
while i in self.black:
8-
i += 1
9-
self.black.add(i)
10-
return i
11-
12-
def addBack(self, num: int) -> None:
13-
self.black.discard(num)
14-
15-
16-
# Your SmallestInfiniteSet object will be instantiated and called as such:
17-
# obj = SmallestInfiniteSet()
18-
# param_1 = obj.popSmallest()
19-
# obj.addBack(num)
1+
from sortedcontainers import SortedSet
2+
3+
4+
class SmallestInfiniteSet:
5+
def __init__(self):
6+
self.s = SortedSet(range(1, 1001))
7+
8+
def popSmallest(self) -> int:
9+
x = self.s[0]
10+
self.s.remove(x)
11+
return x
12+
13+
def addBack(self, num: int) -> None:
14+
self.s.add(num)
15+
16+
17+
# Your SmallestInfiniteSet object will be instantiated and called as such:
18+
# obj = SmallestInfiniteSet()
19+
# param_1 = obj.popSmallest()
20+
# obj.addBack(num)

solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1+
use std::collections::BTreeSet;
2+
13
struct SmallestInfiniteSet {
2-
counter: [bool; 1000],
4+
s: BTreeSet<i32>,
35
}
46

5-
/**
6-
* `&self` means the method takes an immutable reference.
7-
* If you need a mutable reference, change it to `&mut self` instead.
8-
*/
97
impl SmallestInfiniteSet {
108
fn new() -> Self {
11-
Self {
12-
counter: [true; 1000],
9+
let mut set = BTreeSet::new();
10+
for i in 1..=1000 {
11+
set.insert(i);
1312
}
13+
SmallestInfiniteSet { s: set }
1414
}
1515

1616
fn pop_smallest(&mut self) -> i32 {
17-
for i in 0..1000 {
18-
if self.counter[i] {
19-
self.counter[i] = false;
20-
return (i as i32) + 1;
21-
}
22-
}
23-
-1
17+
let x = *self.s.iter().next().unwrap();
18+
self.s.remove(&x);
19+
x
2420
}
2521

2622
fn add_back(&mut self, num: i32) {
27-
self.counter[(num as usize) - 1] = true;
23+
self.s.insert(num);
2824
}
2925
}/**
3026
* Your SmallestInfiniteSet object will be instantiated and called as such:

solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
class SmallestInfiniteSet {
2-
private hashMap: boolean[];
2+
private pq: typeof MinPriorityQueue;
3+
private s: Set<number>;
34

45
constructor() {
5-
this.hashMap = new Array(1001).fill(true);
6+
this.pq = new MinPriorityQueue();
7+
this.s = new Set();
8+
for (let i = 1; i <= 1000; i++) {
9+
this.pq.enqueue(i, i);
10+
this.s.add(i);
11+
}
612
}
713

814
popSmallest(): number {
9-
for (let i = 1; i <= 1001; i++) {
10-
if (this.hashMap[i]) {
11-
this.hashMap[i] = false;
12-
return i;
13-
}
14-
}
15-
return -1;
15+
const x = this.pq.dequeue()?.element;
16+
this.s.delete(x);
17+
return x;
1618
}
1719

1820
addBack(num: number): void {
19-
if (!this.hashMap[num]) {
20-
this.hashMap[num] = true;
21+
if (!this.s.has(num)) {
22+
this.pq.enqueue(num, num);
23+
this.s.add(num);
2124
}
2225
}
2326
}

0 commit comments

Comments
 (0)