-
Notifications
You must be signed in to change notification settings - Fork 894
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Array] Add a solution to Majority Element II
- Loading branch information
Yi Gu
committed
Nov 27, 2016
1 parent
2fe9afd
commit 778d80d
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/majority-element-ii/ | ||
* Primary idea: traverse the array and track the majority element accordingly, do not | ||
* forget to verify they are valid after first iteration | ||
* | ||
* Time Complexity: O(n), Space Complexity: O(1) | ||
* | ||
*/ | ||
|
||
class MajorityElementII { | ||
func majorityElement(_ nums: [Int]) -> [Int] { | ||
var num0: Int? | ||
var num1: Int? | ||
var count0 = 0 | ||
var count1 = 0 | ||
var res = [Int]() | ||
|
||
for num in nums { | ||
if let num0 = num0, num0 == num { | ||
count0 += 1 | ||
} else if let num1 = num1, num1 == num { | ||
count1 += 1 | ||
} else if count0 == 0 { | ||
num0 = num | ||
count0 = 1 | ||
} else if count1 == 0 { | ||
num1 = num | ||
count1 = 1 | ||
} else { | ||
count0 -= 1 | ||
count1 -= 1 | ||
} | ||
} | ||
|
||
count0 = 0 | ||
count1 = 0 | ||
|
||
for num in nums { | ||
if num == num0 { | ||
count0 += 1 | ||
} | ||
if num == num1 { | ||
count1 += 1 | ||
} | ||
} | ||
|
||
if count0 > nums.count / 3 { | ||
res.append(num0!) | ||
} | ||
if count1 > nums.count / 3 { | ||
res.append(num1!) | ||
} | ||
|
||
return res | ||
} | ||
} |