[Codewars] Twice linear

Twice linear

  • 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
24
25
#include <bits/stdc++.h>
using namespace std;
class DoubleLinear
{
public:
static int dblLinear(int n);
};
int DoubleLinear::dblLinear(int n) {
unordered_set<int> us;
priority_queue<int, vector<int>, greater<int>> q;
q.push(1);
auto insert = [&](int x) {
if(!us.count(x)) {
us.insert(x);
q.push(x);
}
};
while(n--) {
auto u = q.top(); q.pop();
us.erase(u);
insert(u * 2 + 1);
insert(u * 3 + 1);
}
return q.top();
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/07/PS/Codewars/twice-linear/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.