-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2336_smallest_number_in_infinite_set.go
More file actions
49 lines (40 loc) · 1.02 KB
/
s2336_smallest_number_in_infinite_set.go
File metadata and controls
49 lines (40 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
https://leetcode.com/problems/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.
*/
//nolint:revive // it's ok
package solutions
type SmallestInfiniteSet struct {
x int
m map[int]bool
}
// NewSmallestInfiniteSet should call Constructor to pass LeetCode test
func NewSmallestInfiniteSet() SmallestInfiniteSet {
return SmallestInfiniteSet{
x: 1,
m: make(map[int]bool),
}
}
func (s *SmallestInfiniteSet) PopSmallest() int {
n := s.x
for s.m[n] {
s.x++
n++
}
s.m[n] = true
s.x++
return n
}
func (s *SmallestInfiniteSet) AddBack(num int) {
if num < s.x {
s.x = num
}
s.m[num] = false
}