-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
313.java
54 lines (54 loc) · 1.72 KB
/
313.java
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
__________________________________________________________________________________________________
sample 7 ms submission
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
int[] uglys = new int[n];
int[] uglyLists = new int[primes.length];
int[] uglyIndexs = new int[primes.length];
uglys[0] = 1;
for(int i=0;i<primes.length;i++){
uglyLists[i] = primes[i];
}
for(int i=1;i<n;i++){
uglys[i] = uglyLists[0];
for(int j=1;j<primes.length;j++){
if(uglyLists[j]<uglys[i]){
uglys[i] = uglyLists[j];
}
}
for(int j=0;j<primes.length;j++){
if(uglyLists[j]==uglys[i]){
uglyIndexs[j]++;
uglyLists[j] = uglys[uglyIndexs[j]]*primes[j];
}
}
}
return uglys[n-1];
}
}
__________________________________________________________________________________________________
sample 33720 kb submission
class Solution {
public static int nthSuperUglyNumber(int n, int[] primes) {
if (n <= 0 || primes == null || primes.length == 0)
return 0;
int len = primes.length;
int[] index = new int[len];
int[] result = new int[n];
result[0] = 1;
for (int i = 1; i < n; i++) {
int min = Integer.MAX_VALUE;
for (int j = 0; j < len; j++) {
if (result[index[j]] * primes[j] < min)
min = result[index[j]] * primes[j];
}
result[i] = min;
for (int j = 0; j < len; j++) {
while (result[index[j]] * primes[j] <= min)
index[j]++;
}
}
return result[n - 1];
}
}
__________________________________________________________________________________________________