-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path313.cpp
More file actions
33 lines (31 loc) · 752 Bytes
/
Copy path313.cpp
File metadata and controls
33 lines (31 loc) · 752 Bytes
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
#include "common.h"
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<long long> dp(n + 1);
vector<long long> nums(primes.begin(), primes.end());
vector<int> points(primes.size(), 1);
dp[1] = 1;
for (auto i = 2; i <= n; ++i) {
int min = INT_MAX;
for (auto j = 0; j < nums.size(); ++j) {
if (nums[j] < min) {
min = nums[j];
}
}
dp[i] = min;
for (auto j = 0; j < nums.size(); ++j) {
if (nums[j] == dp[i]) {
points[j] += 1;
nums[j] = dp[points[j]] * primes[j];
}
}
}
return dp[n];
}
};
int main() {
Solution s;
vector<int> v = {2, 7, 13, 19};
cout << s.nthSuperUglyNumber(12, v);
}