Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f1a2af8

Browse files
committed
Add solution #313
1 parent 99ab8bb commit f1a2af8

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

0313-super-ugly-number.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 313. Super Ugly Number
3+
* https://leetcode.com/problems/super-ugly-number/
4+
* Difficulty: Medium
5+
*
6+
* A super ugly number is a positive integer whose prime factors are in the array primes.
7+
*
8+
* Given an integer n and an array of integers primes, return the nth super ugly number.
9+
*
10+
* The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
11+
*/
12+
13+
/**
14+
* @param {number} n
15+
* @param {number[]} primes
16+
* @return {number}
17+
*/
18+
var nthSuperUglyNumber = function(n, primes) {
19+
const result = [1];
20+
const pointers = new Array(primes.length).fill(0);
21+
const next = [...primes];
22+
23+
while (result.length < n) {
24+
const min = Math.min(...next);
25+
result.push(min);
26+
27+
for (let i = 0; i < primes.length; i++) {
28+
if (next[i] === min) {
29+
pointers[i]++;
30+
next[i] = primes[i] * result[pointers[i]];
31+
}
32+
}
33+
}
34+
35+
return result[n - 1];
36+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@
254254
309|[Best Time to Buy and Sell Stock with Cooldown](./0309-best-time-to-buy-and-sell-stock-with-cooldown.js)|Medium|
255255
310|[Minimum Height Trees](./0310-minimum-height-trees.js)|Medium|
256256
312|[Burst Balloons](./0312-burst-balloons.js)|Hard|
257+
313|[Super Ugly Number](./0313-super-ugly-number.js)|Medium|
257258
315|[Count of Smaller Numbers After Self](./0315-count-of-smaller-numbers-after-self.js)|Hard|
258259
316|[Remove Duplicate Letters](./0316-remove-duplicate-letters.js)|Medium|
259260
318|[Maximum Product of Word Lengths](./0318-maximum-product-of-word-lengths.js)|Medium|

0 commit comments

Comments
 (0)