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?
n개의 정수값이 들어있는 배열에, [n / 2]개 보다 많이 들어있는 값이 있다. 해당 값을 찾아라.
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
class Solution {
public:
// get Kth biggest value by bubble sort
int getKthByBubbleSort(vector%3Cint%3E& nums, int startIndex, int endIndex, int k) {
// simple bubble sort
for (int i = 0; i < endIndex - startIndex - 1; i++) {
for (int j = startIndex; j < endIndex - 1 - i; j++) {
if (nums[j] > nums[j+1]) {
swap(nums[j], nums[j+1]);
}
}
}
return nums[endIndex - k];
}
// partition by pivotvalue (smaller values to left, bigger values to right of the pivot)
pair<int, int> partitionByPivot(vector<int>& nums, int startIndex, int endIndex, int pivotValue) {
int cursor = startIndex;
for (int i = startIndex; i < endIndex; i++) {
if (nums[i] > pivotValue) {
swap(nums[i], nums[endIndex - 1]);
endIndex--;
i--;
} else if (nums[i] < pivotValue) {
swap(nums[cursor], nums[i]);
cursor++;
}
}
int cursorStart = cursor;
while(cursor < endIndex && nums[cursor] == pivotValue) cursor++;
int cursorEnd = cursor - 1;
return make_pair(cursorStart, cursorEnd);
}
// get Kth biggest value in O(n)
int getKthByMedianOfMedians(vector<int>& nums, int startIndex, int endIndex, int k) {
if (endIndex - startIndex <= 5) {
return getKthByBubbleSort(nums, startIndex, endIndex, k);
}
vector<int> medians;
for(int i = startIndex; i < endIndex; i += 5) {
medians.push_back(
getKthByBubbleSort(nums,
i,
min(i + 5, endIndex),
min(3 , ((endIndex - i) / 2) + 1)
)
);
}
int medianOfMedians = getKthByMedianOfMedians(medians, 0, medians.size(), (medians.size() / 2) + 1);
// pivot is medianOfMedians
pair<int, int> indexOfPivot = partitionByPivot(nums, startIndex, endIndex, medianOfMedians);
if (indexOfPivot.first <= endIndex - k && endIndex - k <= indexOfPivot.second) {
return medianOfMedians;
} else if (indexOfPivot.second < endIndex - k) {
return getKthByMedianOfMedians(nums, indexOfPivot.second + 1, endIndex, k);
} else {
return getKthByMedianOfMedians(nums, startIndex, indexOfPivot.first, k - (endIndex - indexOfPivot.first));
}
}
int majorityElement(vector<int>& nums) {
return getKthByMedianOfMedians(nums, 0, nums.size(), (nums.size() / 2) + 1);
}
}
![[Pasted image 20240708231036.png]]{ :width=”500”}
Follow-up: Could you solve the problem in linear time and in
O(1) space?
median of medians 알고리즘을 직접 구현해보면서 느낀 건데, 구현 정확도나 속도가 정말 이전에 비해 많이 떨어진 것 같아요. 구현하는 문제를 많이 풀어야겠어요… 후아후아.
그래도 구현이… 성취감은… 엄청나네요 헿
오늘도 한 건 했다. 꾸준히 할게요 화이팅