-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution313.cs
43 lines (37 loc) · 1.17 KB
/
Solution313.cs
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
namespace LeetCode.Solutions;
public class Solution313
{
/// <summary>
/// 313. Super Ugly Number - Medium
/// <a href="https://leetcode.com/problems/super-ugly-number">See the problem</a>
/// </summary>
public int NthSuperUglyNumber(int n, int[] primes)
{
var uglyNumbers = new int[n];
var indices = new int[primes.Length];
var nextMultiple = new int[primes.Length];
uglyNumbers[0] = 1;
for (var i = 0; i < primes.Length; i++)
{
nextMultiple[i] = primes[i];
}
for (var i = 1; i < n; i++)
{
var nextUglyNumber = int.MaxValue;
for (var j = 0; j < primes.Length; j++)
{
nextUglyNumber = Math.Min(nextUglyNumber, nextMultiple[j]);
}
uglyNumbers[i] = nextUglyNumber;
for (var j = 0; j < primes.Length; j++)
{
if (nextUglyNumber == nextMultiple[j])
{
indices[j]++;
nextMultiple[j] = uglyNumbers[indices[j]] * primes[j];
}
}
}
return uglyNumbers[n - 1];
}
}