-
[LeetCode] 1481. Least Number of Unique Integers after K RemovalsLeetCode 2021. 11. 1. 16:40728x90
https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/
Least Number of Unique Integers after K Removals - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Least Number of Unique Integers after K Removals - Medium

배열에서 k개의 숫자를 지웠을때 남아있는 수에서 각각의 숫자의 개수 중 최소값을 출력하는 문제다.
hash map을 이용해 각각의 숫자의 개수를 저장하고,
priority queue를 이용해 k개 만큼 지워주자.
전체 풀이 코드
class Solution { public: struct compare{bool operator()(pair<int,int> a, pair<int,int> b) {return a.second>b.second;}}; int findLeastNumOfUniqueInts(vector<int>& arr, int k) { unordered_map<int, int> m; priority_queue<pair<int,int>, vector<pair<int,int>>, compare> pq; int N=0, ret=0; for (int n:arr) { if (m.find(n)==m.end()) m[n]=0,N++; m[n]++; } for (auto n:m) pq.push(make_pair(n.first,n.second)); while(k) { pair<int, int> p = pq.top(); if (k>=p.second) k-=p.second, pq.pop(); else break; } return pq.size(); } };'LeetCode' 카테고리의 다른 글
[LeetCode] 404. Sum of Left Leaves (0) 2021.11.04 [LeetCode] 129. Sum Root to Leaf Numbers (0) 2021.11.03 [LeetCode] 1047. Remove All Adjacent Duplicates In String (0) 2021.11.01 [LeetCode] 130. Surrounded Regions (0) 2021.11.01 [LeetCode] 994. Rotting Oranges (0) 2021.10.29