-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0313-super-ugly-number.js
More file actions
36 lines (32 loc) · 886 Bytes
/
0313-super-ugly-number.js
File metadata and controls
36 lines (32 loc) · 886 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
34
35
36
/**
* 313. Super Ugly Number
* https://leetcode.com/problems/super-ugly-number/
* Difficulty: Medium
*
* A super ugly number is a positive integer whose prime factors are in the array primes.
*
* Given an integer n and an array of integers primes, return the nth super ugly number.
*
* The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
*/
/**
* @param {number} n
* @param {number[]} primes
* @return {number}
*/
var nthSuperUglyNumber = function(n, primes) {
const result = [1];
const pointers = new Array(primes.length).fill(0);
const next = [...primes];
while (result.length < n) {
const min = Math.min(...next);
result.push(min);
for (let i = 0; i < primes.length; i++) {
if (next[i] === min) {
pointers[i]++;
next[i] = primes[i] * result[pointers[i]];
}
}
}
return result[n - 1];
};