-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2780.cpp
More file actions
30 lines (30 loc) · 794 Bytes
/
2780.cpp
File metadata and controls
30 lines (30 loc) · 794 Bytes
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
class Solution {
public:
int minimumIndex(vector<int>& nums) {
unordered_map<int, int> mp;
for (auto& num : nums) mp[num]++;
int maxElement = -1;
int maxCnt = -1;
for (auto& [num, cnt] : mp) {
if (cnt > maxCnt) {
maxElement = num;
maxCnt = cnt;
}
}
int left = 0;
int right = maxCnt;
int n = nums.size();
for (int i = 0; i < n - 1; ++i) {
// left: [0, i]
// right: [i + 1, n - 1]
if (nums[i] == maxElement) {
left++;
right--;
}
if ((left * 2 > (i + 1)) && (right * 2 > (n - i - 1))) {
return i;
}
}
return -1;
}
};