[프로그래머스] [C++] 완주하지 못한 선수 (map)

2021. 3. 5. 18:48알고리즘/프로그래머스

1. 문제

 

참가자 명단과

 

완주한 참가자 명단이 주어질때

 

완주하지 못한 참가자 한명을 찾아라

 

단, 동명이인이 존재할 수 있다.

 

2. map

map 의 valu값을 동명이인을 가진 사람의 수로 두었다.

 

초기값을 1로 설정해두고 참가자의 값을 -1 씩하는 것이다.

 

그러면 0이 아닌 참가자가 정답이 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <vector>
#include <map>
 
using namespace std;
 
string solution(vector<string> participant, vector<string> completion) {
    map<stringint> dic;
    std::pair<std::map<string,int>::iterator,bool> isFail;
 
    for(int i = 0; i < participant.size(); i++)
    {
        isFail = dic.insert ( std::pair<string,int>(participant[i],1) );
        if(isFail.second == false)
            dic[participant[i]]++;
    }
 
    for(int i = 0; i < completion.size(); i++)
        dic[completion[i]]--;
 
    for(int i = 0; i < participant.size(); i++)
        if(dic[participant[i]] != 0)
            return participant[i];
 
    return "";
}
cs

programmers.co.kr/learn/courses/30/lessons/42576

 

코딩테스트 연습 - 완주하지 못한 선수

수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수

programmers.co.kr

 

github.com/Nor-s/Algorithm/blob/master/programmers/UTRKA.cpp