[프로그래머스] 귤 고르기
풀이
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int solution(int k, vector<int> tangerine) {
map<int, int> sizeMap = map<int, int>();
for(int i = 0; i<tangerine.size(); ++i){
int t = tangerine[i];
if(sizeMap.find(t) == sizeMap.end()) sizeMap[t] = 1;
else sizeMap[t] += 1;
}
vector<int> sizeCnt = vector<int>();
for(auto it = sizeMap.begin(); it != sizeMap.end(); ++it){
sizeCnt.push_back(it->second);
}
sort(sizeCnt.begin(), sizeCnt.end());
reverse(sizeCnt.begin(), sizeCnt.end());
int answer = 0;
int totalCnt = 0;
for(int i = 0; i < sizeCnt.size(); ++i){
if(totalCnt >= k) break;
totalCnt += sizeCnt[i];
answer++;
}
return answer;
}