Posts [프로그래머스] 완주하지 못한 선수 / C++
Post
Cancel

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


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


unordered_map(hash map) 사용

선수 이름을 key값으로 받아 value 값으로 카운트를 해줍니다.

이 때, 완주한 선수를 다시 카운트 -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
#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";
    unordered_map<string,int> M;
    
    for(string name : participant){
        M[name]++;
    }
    
    for(string name : completion){
        M[name]--;
    }
    
    for(string name : participant){
        if(M[name]>0)
            answer = name;
    }
    
    return answer;
}



This post is licensed under CC BY 4.0 by the author.

[프로그래머스] 서울에서 김서방 찾기 / C++

[OS 운영체제] 운영체제 및 컴퓨터 시스템 기본 요소

Comments powered by Disqus.