Programmers Review

[Lv 1] 완주하기 못한 선수

hanseongbugi 2024. 6. 1. 15:44

https://school.programmers.co.kr/learn/courses/30/lessons/42576#

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

이 문제는 map을 사용해서 해결하였다.

완주한 선수의 목록을 map을 통해 정리한다. 이때 동명이인이 있을 수 있으니 1이상의 수로 정리한다.

 

마지막으로 선수 목록을 순회하며 map에 1미만의 값을 갖는 선수를 찾는다.

동명이인이 있을 수 있으니 map의 값이 있는 경우 1을 감소시킨다.

 

#include <string>
#include <vector>
#include <map>
using namespace std;

int N, M;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";
    map<string,int> m;
    N = completion.size();
    M = participant.size();
    
    for(int i = 0;i<N;i++){
        string name = completion[i];
        if(m[name] > 0)
            m[name] += 1;
        else{
            m[name] = 1;
        }
    }
    for(int i = 0;i<M;i++){
        string name = participant[i];
        if(m[name] < 1){
            answer += name;
        }
        else{
            m[name]--;
        }
    }

    return answer;
}