[프로그래머스] 완주하지 못한 선수

Zoo Da·2021년 11월 12일
0

프로그래머스

목록 보기
7/10
post-thumbnail

링크

https://programmers.co.kr/learn/courses/30/lessons/42576?language=cpp

sol1) multiset

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>

using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";
    multiset<string> MS;
    for(auto& c : participant) MS.insert(c);
    for(auto& s : completion) if(MS.count(s)) MS.erase(MS.find(s));
    answer = *MS.begin();
    return answer;
}

set을 사용, 참여한 선수들의 이름을 전부 set에 저장하고 완주한 선수들의 이름을 set.count를 사용해서 확인 후 지워준다.
동명이인이 있을 수 있기 때문에 multi_set을 사용해야하며, 이름을 지울 때 MS.erase(이름) 형식으로 지운다면 set에 있는 같은 이름을 전부 지우기 때문에 find를 써서 이터레이터를 넘겨줘야합니다.

profile
메모장 겸 블로그

0개의 댓글