-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
69 lines (56 loc) · 1.54 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n / 3 ⌋ times.
Note: The algorithm should run in linear time and in O(1) space.
Time complexity: O(n)
Space complexity: O(1)
*/
class Solution {
func majorityElement(_ nums: [Int]) -> [Int] {
var majority1 = 0
var count1 = 0
var majority2 = 0
var count2 = 0
for num in nums {
if num == majority1 {
count1 += 1
continue
}
if num == majority2 {
count2 += 1
continue
}
if count1 == 0 {
majority1 = num
count1 += 1
continue
}
if count2 == 0 {
majority2 = num
count2 += 1
continue
}
count1 -= 1
count2 -= 1
}
var result = [Int]()
let target = nums.count / 3
count1 = 0
count2 = 0
for num in nums {
if num == majority1 {
count1 += 1
}
if num == majority2 {
count2 += 1
}
}
if count1 > target {
result.append(majority1)
}
if majority2 != majority1, count2 > target {
result.append(majority2)
}
return result
}
}