Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
903 changes: 722 additions & 181 deletions solution/2300-2399/2336.Smallest Number in Infinite Set/README.md

Large diffs are not rendered by default.

901 changes: 723 additions & 178 deletions solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
class SmallestInfiniteSet {
public:
unordered_set<int> black;

SmallestInfiniteSet() {
}

int popSmallest() {
int i = 1;
for (; black.count(i); ++i)
;
black.insert(i);
return i;
}

void addBack(int num) {
black.erase(num);
}
};

/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet* obj = new SmallestInfiniteSet();
* int param_1 = obj->popSmallest();
* obj->addBack(num);
class SmallestInfiniteSet {
public:
SmallestInfiniteSet() {
for (int i = 1; i <= 1000; ++i) {
s.insert(i);
}
}

int popSmallest() {
int x = *s.begin();
s.erase(s.begin());
return x;
}

void addBack(int num) {
s.insert(num);
}

private:
set<int> s;
};

/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet* obj = new SmallestInfiniteSet();
* int param_1 = obj->popSmallest();
* obj->addBack(num);
*/
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
type SmallestInfiniteSet struct {
black map[int]bool
s *treemap.Map
}

func Constructor() SmallestInfiniteSet {
s := map[int]bool{}
s := treemap.NewWithIntComparator()
for i := 1; i <= 1000; i++ {
s.Put(i, nil)
}
return SmallestInfiniteSet{s}
}

func (this *SmallestInfiniteSet) PopSmallest() int {
i := 1
for ; this.black[i]; i++ {
}
this.black[i] = true
return i
x, _ := this.s.Min()
this.s.Remove(x.(int))
return x.(int)
}

func (this *SmallestInfiniteSet) AddBack(num int) {
this.black[num] = false
this.s.Put(num, nil)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
class SmallestInfiniteSet {
private Set<Integer> black = new HashSet<>();

public SmallestInfiniteSet() {
}

public int popSmallest() {
int i = 1;
for (; black.contains(i); ++i)
;
black.add(i);
return i;
}

public void addBack(int num) {
black.remove(num);
}
}

/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
* int param_1 = obj.popSmallest();
* obj.addBack(num);
class SmallestInfiniteSet {
private TreeSet<Integer> s = new TreeSet<>();

public SmallestInfiniteSet() {
for (int i = 1; i <= 1000; ++i) {
s.add(i);
}
}

public int popSmallest() {
return s.pollFirst();
}

public void addBack(int num) {
s.add(num);
}
}

/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
* int param_1 = obj.popSmallest();
* obj.addBack(num);
*/
39 changes: 20 additions & 19 deletions solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
class SmallestInfiniteSet:
def __init__(self):
self.black = set()

def popSmallest(self) -> int:
i = 1
while i in self.black:
i += 1
self.black.add(i)
return i

def addBack(self, num: int) -> None:
self.black.discard(num)


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


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)
26 changes: 11 additions & 15 deletions solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
use std::collections::BTreeSet;

struct SmallestInfiniteSet {
counter: [bool; 1000],
s: BTreeSet<i32>,
}

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl SmallestInfiniteSet {
fn new() -> Self {
Self {
counter: [true; 1000],
let mut set = BTreeSet::new();
for i in 1..=1000 {
set.insert(i);
}
SmallestInfiniteSet { s: set }
}

fn pop_smallest(&mut self) -> i32 {
for i in 0..1000 {
if self.counter[i] {
self.counter[i] = false;
return (i as i32) + 1;
}
}
-1
let x = *self.s.iter().next().unwrap();
self.s.remove(&x);
x
}

fn add_back(&mut self, num: i32) {
self.counter[(num as usize) - 1] = true;
self.s.insert(num);
}
}/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
Expand Down
25 changes: 14 additions & 11 deletions solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
class SmallestInfiniteSet {
private hashMap: boolean[];
private pq: typeof MinPriorityQueue;
private s: Set<number>;

constructor() {
this.hashMap = new Array(1001).fill(true);
this.pq = new MinPriorityQueue();
this.s = new Set();
for (let i = 1; i <= 1000; i++) {
this.pq.enqueue(i, i);
this.s.add(i);
}
}

popSmallest(): number {
for (let i = 1; i <= 1001; i++) {
if (this.hashMap[i]) {
this.hashMap[i] = false;
return i;
}
}
return -1;
const x = this.pq.dequeue()?.element;
this.s.delete(x);
return x;
}

addBack(num: number): void {
if (!this.hashMap[num]) {
this.hashMap[num] = true;
if (!this.s.has(num)) {
this.pq.enqueue(num, num);
this.s.add(num);
}
}
}
Expand Down