출처 : https://programmers.co.kr/learn/courses/30/lessons/1845#

 

코딩테스트 연습 - 폰켓몬

당신은 폰켓몬을 잡기 위한 오랜 여행 끝에, 홍 박사님의 연구실에 도착했습니다. 홍 박사님은 당신에게 자신의 연구실에 있는 총 N 마리의 폰켓몬 중에서 N/2마리를 가져가도 좋다고 했습니다.

programmers.co.kr

 

 

hash map을 이용해 종류개수를 구하는 것이 핵심이다.

 

#include <vector>
#include <map>
using namespace std;

int solution(vector<int> nums)
{
    int nSize = nums.size() / 2;
    map<int, int> mapping;
    
    for (auto it : nums) {
        mapping[it] += 1;
    }
    
    return nSize > mapping.size() ? mapping.size() : nSize;
        
}

+ Recent posts