-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsensus.java
72 lines (57 loc) · 1.76 KB
/
Consensus.java
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Set;
/**
* Indeed
* <p>
* Given n iterators. Collect only those numbers that occur in more than k iterators.
* <p>
* #Medium #Iterator #Interview
* #Incomplete
*/
public class Consensus {
public static void main(String[] args) {
}
static class MyIterator implements Iterator<Integer> {
public Integer peek() {
return 0;
}
@Override
public boolean hasNext() {
return false;
}
@Override
public Integer next() {
return null;
}
}
List<Integer> findConsensus(List<MyIterator> data, int k) {
if (data == null || data.isEmpty() || k < 1) return Collections.emptyList();
List<Integer> result = new ArrayList<>();
PriorityQueue<MyIterator> pq = new PriorityQueue<>(Comparator.comparingInt(MyIterator::peek));
pq.addAll(data);
while (!pq.isEmpty()) {
MyIterator currMin = pq.poll();
Set<MyIterator> same = new HashSet<>();
for (MyIterator it : pq) {
if (it.peek().equals(currMin.peek())) {
same.add(it);
}
}
if (same.size() >= k) result.add(currMin.peek());
while (!pq.isEmpty()) {
final MyIterator it = pq.poll();
final Integer current = it.peek();
if (current.equals(currMin.peek())) {
while (it.hasNext() && it.peek().equals(current)) it.next();
}
}
}
return result;
}
}