forked from WenchaoD/LeetCode_Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UglyNumberII.swift
38 lines (31 loc) · 965 Bytes
/
UglyNumberII.swift
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
/**
* Question Link: https://leetcode.com/problems/ugly-number-ii/
* Primary idea: Use three indices to build a helper array
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class UglyNumberII {
func nthUglyNumber(n: Int) -> Int {
guard n > 1 else {
return 1
}
var uglyNums = [Int](count: n, repeatedValue: 1)
var index2 = 0
var index3 = 0
var index5 = 0
for i in 1 ..< n {
var minVal = min(uglyNums[index2] * 2, uglyNums[index3] * 3, uglyNums[index5] * 5)
if minVal == uglyNums[index2] * 2 {
index2 += 1
}
if minVal == uglyNums[index3] * 3 {
index3 += 1
}
if minVal == uglyNums[index5] * 5{
index5 += 1
}
uglyNums[i] = minVal
}
return uglyNums[n - 1]
}
}