File tree 8 files changed +1549
-461
lines changed
solution/2300-2399/2336.Smallest Number in Infinite Set
8 files changed +1549
-461
lines changed Load Diff Large diffs are not rendered by default.
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 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);
26
28
*/
Original file line number Diff line number Diff line change 1
1
type SmallestInfiniteSet struct {
2
- black map [ int ] bool
2
+ s * treemap. Map
3
3
}
4
4
5
5
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
+ }
7
10
return SmallestInfiniteSet {s }
8
11
}
9
12
10
13
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 )
16
17
}
17
18
18
19
func (this * SmallestInfiniteSet ) AddBack (num int ) {
19
- this .black [ num ] = false
20
+ this .s . Put ( num , nil )
20
21
}
21
22
22
23
/**
Original file line number Diff line number Diff line change 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);
25
24
*/
Original file line number Diff line number Diff line change 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)
Original file line number Diff line number Diff line change
1
+ use std:: collections:: BTreeSet ;
2
+
1
3
struct SmallestInfiniteSet {
2
- counter : [ bool ; 1000 ] ,
4
+ s : BTreeSet < i32 > ,
3
5
}
4
6
5
- /**
6
- * `&self` means the method takes an immutable reference.
7
- * If you need a mutable reference, change it to `&mut self` instead.
8
- */
9
7
impl SmallestInfiniteSet {
10
8
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) ;
13
12
}
13
+ SmallestInfiniteSet { s : set }
14
14
}
15
15
16
16
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
24
20
}
25
21
26
22
fn add_back ( & mut self , num : i32 ) {
27
- self . counter [ ( num as usize ) - 1 ] = true ;
23
+ self . s . insert ( num) ;
28
24
}
29
25
} /**
30
26
* Your SmallestInfiniteSet object will be instantiated and called as such:
Original file line number Diff line number Diff line change 1
1
class SmallestInfiniteSet {
2
- private hashMap : boolean [ ] ;
2
+ private pq : typeof MinPriorityQueue ;
3
+ private s : Set < number > ;
3
4
4
5
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
+ }
6
12
}
7
13
8
14
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 ;
16
18
}
17
19
18
20
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 ) ;
21
24
}
22
25
}
23
26
}
You can’t perform that action at this time.
0 commit comments