문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/1845
코딩테스트 연습 - 폰켓몬
당신은 폰켓몬을 잡기 위한 오랜 여행 끝에, 홍 박사님의 연구실에 도착했습니다. 홍 박사님은 당신에게 자신의 연구실에 있는 총 N 마리의 폰켓몬 중에서 N/2마리를 가져가도 좋다고 했습니다.
programmers.co.kr
N/2와 주어진 폰켓몬 종류 중 작은 값을 반환하면 되는 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <map> | |
using namespace std; | |
int solution(vector<int> nums) | |
{ | |
int answer = nums.size() / 2; | |
map<int, bool> visited; | |
int cnt = 0; | |
for (int num : nums) | |
{ | |
if (visited.count(num)) | |
{ | |
continue; | |
} | |
visited[num] = true; | |
cnt++; | |
} | |
return min(answer, cnt); | |
} | |
int main(void) | |
{ | |
vector<int> nums = { 3, 1, 2, 3 }; | |
cout << solution(nums) << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 사칙연산 (0) | 2021.09.30 |
---|---|
[Programmers] 게임 맵 최단거리 (0) | 2021.09.30 |
[Programmers] 단어 퍼즐 (0) | 2021.09.30 |
[Programmers] 예상 대진표 (0) | 2021.09.30 |
[Programmers] 짝지어 제거하기 (0) | 2021.09.30 |