[InterviewBit] Smallest sequence with given Primes

Smallest sequence with given Primes

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vector<int> Solution::solve(int A, int B, int C, int D) {
unordered_set<int> vis;
priority_queue<int, vector<int>, greater<int>> q;
auto push = [&](int n) {
if(!vis.count(n)) {
vis.insert(n);
q.push(n);
}
};
push(A);
push(B);
push(C);
vector<int> res;
while(res.size() < D) {
auto u = q.top(); q.pop();
res.push_back(u);
push(A * u);
push(B * u);
push(C * u);
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/19/PS/interviewbit/smallest-sequence-with-given-primes/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.