https://programmers.co.kr/learn/courses/30/lessons/42576
코딩테스트 연습 - 완주하지 못한 선수
수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수
programmers.co.kr
중복된 이름을 가진 선수가 있을 수 있으므로, multiset이 적합합니다.
참가한 선수들의 이름을 multiset에 저장하고, 완주한 선수들의 이름이 저장된 vector를 for-each 순회를 시키면서 multiset에 저장된 선수들을 지워줍니다.
최종적으로, 완주하지 못한 선수만이 multiset에 남아 있게 됩니다.
#include <string>
#include <vector>
#include <unordered_set>
#include <iostream>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
unordered_multiset<string> new_participant(participant.begin(), participant.end());
for (auto& name : completion)
{
new_participant.erase(new_participant.find(name));
}
answer = *new_participant.begin();
return answer;
}
int main()
{
vector<string> participant = { "A", "B", "C" };
vector<string> completion = { "B", "C" };
cout << solution(participant, completion);
}
'코딩 테스트 > 프로그래머스(lv1)' 카테고리의 다른 글
[프로그래머스]키패드 누르기(c++) (0) | 2022.05.16 |
---|---|
[프로그래머스]소수만들기(c++) (0) | 2022.05.16 |
[프로그래머스]평균구하기(c++) (0) | 2022.05.16 |
[프로그래머스]숫자 문자열과 영단어(c++) (0) | 2022.05.16 |