-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKth_Prime_Number.cpp
39 lines (38 loc) · 1.02 KB
/
Kth_Prime_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
/*
Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eligible numbers are like 3, 5, 7, 9, 15 ...
Example
If k=4, return 9.
*/
// in fact, its name is ugly number.
// time O(n)
// min is in algorithm lib and use std::
// use long long
#include <algorithm>
using namespace std;
class Solution {
public:
/*
* @param k: The number k.
* @return: The kth prime number as description.
*/
long long kthPrimeNumber(int k) {
// write your code here
long long dp[k + 1];
dp[0] = 1;
int three = 0, five = 0, seven = 0;
for (int i = 1; i <= k; i++) {
long long next = min(min(dp[three] * 3, dp[five] * 5), dp[seven] * 7);
dp[i] = next;
if (next == dp[three] * 3) {
three++;
}
if (next == dp[five] * 5) {
five++;
}
if (next == dp[seven] * 7) {
seven++;
}
}
return dp[k];
}
};