-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallestNumberInInfiniteSet.java
More file actions
132 lines (104 loc) · 3.52 KB
/
SmallestNumberInInfiniteSet.java
File metadata and controls
132 lines (104 loc) · 3.52 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package Algorithms.HeapAlgos;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 08 May 2025
*/
public class SmallestNumberInInfiniteSet {
public static void main(String[] args) {
SmallestInfiniteSet obj = new SmallestInfiniteSet();
obj.addBack(2);
System.out.println("added: " + 2);
System.out.println("popped: " + obj.popSmallest());
System.out.println("popped: " + obj.popSmallest());
System.out.println("popped: " + obj.popSmallest());
obj.addBack(1);
System.out.println("added: " + 1);
System.out.println("popped: " + obj.popSmallest());
System.out.println("popped: " + obj.popSmallest());
System.out.println("popped: " + obj.popSmallest());
}
/**
* @TimeComplexity O(1) -- worst case scenario is O(n)
* @SpaceComplexity O(n)
*Hea
* Here maintain the data of popped nums in a set
*/
static class SmallestInfiniteSetMyApproach {
int min = 1;
Set<Integer> popped = new HashSet<>();
public int popSmallest(){
int res = min;
while(popped.contains(min+1)) min++; // trav till next smallest num in infinite set i.e that smallest won't present in popped set
min++;
popped.add(res);
return res;
}
public void addBack(int num) {
popped.remove(num);
min = Math.min(min, num);
}
}
/**
* @TimeComplexity O(1)
* @SpaceComplexity O(n)
*
* Here maintain the data of addBacks in minHeap instead of popped. And add to minHeap only if num < smallest && num not present in minHeap
*/
static class SmallestInfiniteSet {
int smallest = 1;
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // to save addBacks or manually inserted
public int popSmallest() {
if(!minHeap.isEmpty()) return minHeap.poll();
return smallest++; // if minHeap is empty
}
public void addBack(int num) {
if(num < smallest && !minHeap.contains(num)) minHeap.offer(num);
}
}
static class SmallestInfiniteSetImproved {
int smallest = 1;
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // to save addBacks
Set<Integer> addBacks = new HashSet<>(); // same eles in minHeap
public int popSmallest() {
if(!minHeap.isEmpty()) {
int res = minHeap.poll();
addBacks.remove(res);
return res;
}
return smallest++;
}
public void addBack(int num) {
if(smallest > num && !addBacks.contains(num)) {
minHeap.offer(num);
addBacks.add(num);
}
}
}
static class SmallestInfiniteSet2 {
int cur;
PriorityQueue<Integer> minHeap;
Set<Integer> addBacks;
public SmallestInfiniteSet2() {
cur = 1;
minHeap = new PriorityQueue<>();
addBacks = new HashSet<>();
}
public int popSmallest() {
if(!minHeap.isEmpty()){
int res = minHeap.poll();
addBacks.remove(res);
return res;
}else{
return cur++;
}
}
public void addBack(int num) {
if(num < cur && addBacks.add(num)){
minHeap.offer(num);
}
}
}
}