-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmajority_element.dart
89 lines (67 loc) · 1.75 KB
/
majority_element.dart
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
-* Majority Element *-
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times.
You may assume that the majority element always exists in the array.
Example 1:
Input: nums = [3,2,3]
Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Constraints:
n == nums.length
1 <= n <= 5 * 104
-109 <= nums[i] <= 109
Follow-up: Could you solve the problem in linear time and in O(1) space?
*/
class A {
// Runtime: 512 ms, faster than 58.33% of Dart online submissions for Majority Element.
// Memory Usage: 147.1 MB, less than 66.67% of Dart online submissions for Majority Element.
int majorityElement(List<int> nums) {
int n = nums.length;
int majEle = nums[0];
int count = 1;
for (int i = 1; i < n; i++) {
if (nums[i] == majEle) {
count++;
} else {
count--;
if (count == 0) {
majEle = nums[i];
count = 1;
}
}
}
return majEle;
}
}
class B {
int majorityElement(List<int> nums) {
Map<int, int> counter = {};
for (int x in nums) {
// null error i will fix soon
if (counter[x++] == null && counter[x++]! > nums.length ~/ 2) {
return x;
}
}
return 0;
}
}
class C {
// Runtime: 505 ms, faster than 61.11% of Dart online submissions for Majority Element.
// Memory Usage: 146.7 MB, less than 94.44% of Dart online submissions for Majority Element.
int majorityElement(List<int> nums) {
int major = nums[0], count = 1;
for (int i = 1; i < nums.length; i++) {
if (count == 0) {
count++;
major = nums[i];
} else if (major == nums[i]) {
count++;
} else
count--;
}
return major;
}
}