forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
super-ugly-number.cpp
141 lines (124 loc) · 4.02 KB
/
super-ugly-number.cpp
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
133
134
135
136
137
138
139
140
141
// Time: O(n * k)
// Space: O(n + k)
// Heap solution. (308ms)
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap;
vector<int> uglies(n), idx(primes.size()), ugly_by_last_prime(n);
uglies[0] = 1;
for (int i = 0; i < primes.size(); ++i) {
heap.emplace(primes[i], i);
}
for (int i = 1; i < n; ++i) {
int k;
tie(uglies[i], k) = heap.top();
heap.pop();
ugly_by_last_prime[i] = k;
while (ugly_by_last_prime[++idx[k]] > k); // average time: O(k)
heap.emplace(uglies[idx[k]] * primes[k], k);
}
return uglies[n - 1];
}
};
// Time: O(n * k)
// Space: O(n + k)
// DP solution. (596ms)
class Solution2 {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<int> uglies(n), ugly_by_prime(primes), idx(primes.size());
uglies[0] = 1;
for (int i = 1; i < n; ++i) {
int min_val = *min_element(ugly_by_prime.begin(), ugly_by_prime.end());
uglies[i] = min_val;
for (int k = 0; k < primes.size(); ++k) {
if (min_val == ugly_by_prime[k]) {
ugly_by_prime[k] = primes[k] * uglies[++idx[k]];
}
}
}
return uglies[n - 1];
}
};
// Time: O(n * logk) ~ O(n * klogk)
// Space: O(k^2)
// Heap solution. (612ms)
class Solution3 {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
long long ugly_number = 0;
priority_queue<long long , vector<long long>, greater<long long>> heap;
heap.emplace(1);
for (const auto& p: primes) {
heap.emplace(p);
}
for (int i = 0; i < n; ++i) {
ugly_number = heap.top();
heap.pop();
int j = 0;
for (; j < primes.size(); ++j) {
if (ugly_number % primes[j] == 0) {
for (int k = 0; k <= j; ++k) {
// worst time: O(klogk)
// worst space: O(k^2)
heap.emplace(ugly_number * primes[k]);
}
break;
}
}
}
return ugly_number;
}
};
// Time: O(n * k)
// Space: O(n + k)
// Hash solution. (804ms)
class Solution4 {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap;
unordered_set<int> ugly_set{1};
vector<int> uglies(n), idx(primes.size());
uglies[0] = 1;
for (int k = 0; k < primes.size(); ++k) {
heap.emplace(primes[k], k);
ugly_set.emplace(primes[k]);
}
for (int i = 1; i < n; ++i) {
int k;
tie(uglies[i], k) = heap.top();
heap.pop();
while (ugly_set.count(primes[k] * uglies[idx[k]])) {
++idx[k];
}
heap.emplace(primes[k] * uglies[idx[k]], k);
ugly_set.emplace(primes[k] * uglies[idx[k]]);
}
return uglies[n - 1];
}
};
// Time: O(n * logk) ~ O(n * klogk)
// Space: O(n + k)
// Heap solution. (1184ms)
class Solution5 {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap;
vector<int> uglies(n), idx(primes.size());
uglies[0] = 1;
for (int k = 0; k < primes.size(); ++k) {
heap.emplace(primes[k], k);
}
for (int i = 1; i < n; ++i) {
int k;
tie(uglies[i], k) = heap.top();
while (heap.top().first == uglies[i]) { // worst time: O(klogk)
tie(uglies[i], k) = heap.top();
heap.pop();
heap.emplace(primes[k] * uglies[++idx[k]], k);
}
}
return uglies[n - 1];
}
};