File tree 2 files changed +37
-0
lines changed 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 254
254
309|[ Best Time to Buy and Sell Stock with Cooldown] ( ./0309-best-time-to-buy-and-sell-stock-with-cooldown.js ) |Medium|
255
255
310|[ Minimum Height Trees] ( ./0310-minimum-height-trees.js ) |Medium|
256
256
312|[ Burst Balloons] ( ./0312-burst-balloons.js ) |Hard|
257
+ 313|[ Super Ugly Number] ( ./0313-super-ugly-number.js ) |Medium|
257
258
315|[ Count of Smaller Numbers After Self] ( ./0315-count-of-smaller-numbers-after-self.js ) |Hard|
258
259
316|[ Remove Duplicate Letters] ( ./0316-remove-duplicate-letters.js ) |Medium|
259
260
318|[ Maximum Product of Word Lengths] ( ./0318-maximum-product-of-word-lengths.js ) |Medium|
You can’t perform that action at this time.
0 commit comments