-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
264 Ugly Number II.swift
48 lines (40 loc) 路 1.05 KB
/
264 Ugly Number II.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
39
40
41
42
43
44
45
46
47
48
//
// 264 Ugly Number II.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 04/07/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/ugly-number-ii/
class Solution {
/// Finds the n-th ugly number.
///
/// - Parameter n: The integer.
/// - Returns: N-th ugly number.
///
/// - Complexity:
/// - time: O(n), where n is given n.
/// - space: O(n), where n is given n.
func nthUglyNumber(_ n: Int) -> Int {
guard n > 0 else { return -1 }
var ans = [Int](repeating: 0, count: n)
ans[0] = 1
var i2 = 0
var i3 = 0
var i5 = 0
for i in 1..<n {
ans[i] = min(ans[i2] * 2, ans[i3] * 3, ans[i5] * 5)
if ans[i] == ans[i2] * 2 {
i2 += 1
}
if ans[i] == ans[i3] * 3 {
i3 += 1
}
if ans[i] == ans[i5] * 5 {
i5 += 1
}
}
return ans[n-1]
}
}