반응형
728x90
반응형
sort(participant.begin(), participant.end());
sort(completion.begin(), completion.end());

for(int i=0; i<participant.size(); i++)
	if(participant[i] != completion[i])
		return participant[i];

#include <algorithm> 의 sort 함수를 이용하여, 참석자 명단과 완주자 명단을 정렬.

그러면 나란히 인덱스를 비교한다. 인덱스를 증가하면서 비교하다가 서로 틀린 부분이 있다면 참석자 측에 해당 인덱스의 사람이 완주를 못한 것이 된다.

또 다른 풀이

#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>

using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";
    
    map<string, int> m;
    
    for(auto e : participant)
    {
        if(m.find(e) == m.end())
            m[e] = 1;
        else
            m[e] += 1;
    }
    for(auto e : completion)
        m[e]--;
    
    for(auto e : participant)
    {
        if(m[e] > 0)
        {
            answer = e;
            break;
        }
    }
    
    return answer;
}

1. 참여자들을 key로 해서 1씩 증가시킨다 

2. 완주자들을 key로 해서 1씩 감소시킨다.

3. 0이 아닌 친구들이 존재하면 그 친구들은 완주하지 못한 것이다 ㅋㅋ

728x90
반응형

'프로그래밍응용 > 오답노트' 카테고리의 다른 글

SQL1  (0) 2021.02.15
무식하게 완전 탐색! - 모의고사  (0) 2021.02.15
벡터 복사 및 sort 이용  (0) 2021.02.13
우선순위 큐 이용한 문제 ㅋㅋ  (0) 2021.02.13
stack 주식 가격  (0) 2021.02.13

+ Recent posts