코딩테스트/프로그래머스
[프로그래머스] 폰켓몬 (C++)
Deff_a
2024. 8. 18. 12:39
문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
set을 이용하여 폰켓몬 Vector의 중복을 제거하고, 최대로 선택 가능한 폰켓몬 수와, set에 저장된 요소들의 수 중 작은 수를 반환한다.
#include <vector>
#include <set>
// https://school.programmers.co.kr/learn/courses/30/lessons/1845
using namespace std;
int solution(vector<int> nums)
{
set<int> uniqueMonsters(nums.begin(), nums.end()); // 중복 제거
int maxPick = nums.size() / 2; // 최대 선택 가능한 폰켓몬 수
return min((int)uniqueMonsters.size(), maxPick); // 종류의 수와 maxPick 중 작은 값 반환
}