-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2336SmallestNumberinInfiniteSet.java
More file actions
50 lines (40 loc) · 1.61 KB
/
Q2336SmallestNumberinInfiniteSet.java
File metadata and controls
50 lines (40 loc) · 1.61 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
50
/*
b-knd (jingru) on 10 July 2022 11:48:00
*/
import java.util.ArrayList;
/*
Question:
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.
*/
public class Q2336SmallestNumberinInfiniteSet {
static class SmallestInfiniteSet {
//keep track of the current smallest element
int smallest = 1;
//keep track of element that have been removed
ArrayList<Integer> removed;
//initiate a new empty arraylist (with no element removed yet)
public SmallestInfiniteSet() {
removed = new ArrayList<>();
}
//remove 'smallest' and find the next smallest element (that has not been removed)
public int popSmallest() {
int pop = smallest;
removed.add(smallest);
while(removed.contains(smallest)){
smallest++;
}
return pop;
}
//if the element has not been removed, replaced 'smallest' with num if num smaller than 'smallest', and removes num from list of removed elements
public void addBack(int num) {
if(removed.contains(num)){
smallest = Math.min(smallest, num);
removed.remove(new Integer(num));
}
}
}
}