새발블로그
[프로그래머스/C++] 폰켓몬 본문
문제
https://school.programmers.co.kr/learn/courses/30/lessons/1845
풀이방법
일단 N/2 마리로 제한을 두고, set을 통해서 고를 수 있는 선택지를 줄였다
그 다음에 N/2보다 set의 길이가 같거나 작을 때 set size를 반환하고,
그렇지 않으면 N/2의 값을 반환하였다.
근데 이 로직을 다른 사람은 좀 더 간결하게 min을 사용해서 표현했길래 주워왔다
풀이1
#include <vector>
#include <set>
using namespace std;
int solution(vector<int> nums)
{
int answer = 0;
int N=nums.size();
int max_choice = N/2;
set<int> s(nums.begin(), nums.end());
if (s.size()<=max_choice)
{
answer = s.size();
}
else
{
answer = max_choice;
}
return answer;
}
풀이2
#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> nums) {
unordered_set<int> s(nums.begin(), nums.end());
return min(nums.size() / 2, s.size());
}
아주..심플하구만
'Problem Solving > Programmers' 카테고리의 다른 글
| [프로그래머스/C++] 같은 숫자는 싫어 (0) | 2024.03.26 |
|---|---|
| [프로그래머스/C++] 수열과 구간 쿼리 (0) | 2024.03.07 |
| [프로그래머스/C++] 수 조작하기2 (0) | 2024.03.07 |
| [프로그래머스/C++] 수 조작하기 1 (0) | 2024.03.06 |
| [프로그래머스/C++] 주사위 게임 2 (0) | 2024.03.05 |