728x90
반응형
class Solution {
public:
static bool cmp(pair<int, int>& a, pair<int, int>& b)
{
return a.second < b.second;
}
int majorityElement(vector<int>& nums) {
int res;
unordered_map<int,int> map;
for(int i=0;i<nums.size();i++)
map[nums[i]]++;
vector<pair<int, int>> v(map.begin(), map.end());
sort(v.rbegin(), v.rend(), cmp);
res = v[0].first;
return res;
}
};
map 으로 담아준뒤 , vector로 정렬해서 첫번째 값을 반환했다.
https://leetcode.com/problems/majority-element/?envType=study-plan&id=data-structure-ii
728x90
반응형
'자라는 개발자 > 문제풀이' 카테고리의 다른 글
c++ 2021 카카오 채용연계형 인턴십 - 숫자 문자열과 영단어 (0) | 2023.08.15 |
---|---|
c++ 백준 27964 콰트로치즈피자 (0) | 2023.05.20 |
leetcode 383. Ransom Note (0) | 2022.11.13 |
leetcode 387. First Unique Character in a String (0) | 2022.11.13 |
leetcode 350. Intersection of Two Arrays II (0) | 2022.11.13 |