leetcode 313. Super Ugly Number
Write a program to find the nth super ugly number.Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k.
Example:
Input: n = 12, primes =
Output: 32
Explanation: is the sequence of the first 12
super ugly numbers given primes = of size 4.
Note:
1 is a super ugly number for any given primes.
The given numbers in primes are in ascending order.
0 < k ≤ 100, 0 < n ≤ 106, 0 < primes < 1000.
The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
int[] arr = new int ;
arr = 1;
HashMap <Integer, Integer> map = new HashMap<>();
for(int i = 0; i< primes.length ; i++){
map.put(primes, 1);
}
for(int i = 2; i <= n ; i++){
int min = Integer.MAX_VALUE;
for(Integer key : map.keySet()){
if(key * arr < min){
min = key * arr;
}
}
for(Integer key : map.keySet()){
if(key * arr == min){
map.put(key, map.get(key)+1);
}
}
arr = min;
}
return arr;
}
}
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
int[] val = new int;
int[] idx = new int;
int[] ugly = new int;
Arrays.fill(val,1);
int next = 1;
for(int i = 0; i < n ; i++){
ugly = next;
next = Integer.MAX_VALUE;
for(int j = 0; j< primes.length;j++){
if(ugly == val) val = primes * ugly++];
next = Math.min(val, next);
}
}
return ugly;
}
}
页:
[1]