-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
414 Third Maximum Number.swift
40 lines (33 loc) 路 1.06 KB
/
414 Third Maximum Number.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
//
// 414 Third Maximum Number.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 15/10/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/third-maximum-number/
class Solution {
/// Finds the third maximum number in the given array. If it does not exist, finds
/// the maximum number.
///
/// - Parameter nums: A non-empty array of integers.
/// - Returns: The third maximum number in the given array, if it does not exist,
/// returns the maximum number.
///
/// - Complexity:
/// - time: O(n), where n is the length of `nums`.
/// - space: O(1), only constant space is used.
func thirdMax(_ nums: [Int]) -> Int {
var top3 = Set<Int>()
for num in nums {
top3.insert(num)
guard top3.count > 3, let min = top3.min() else { continue }
top3.remove(min)
}
guard top3.count == 3 else {
return top3.max() ?? -1
}
return top3.min() ?? -1
}
}