CodingTest Practice/Programmers

프로그래머스 : 완주하지 못한 선수 (C++, Lv.1, 해시)

몽땅마니아(MDD) 2022. 5. 20. 11:57

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

 

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

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

programmers.co.kr

 

해시 문제를 풀 줄 몰라서 블로그에 올라온 정답과 해시에 대한 공부를 하고 문제를 풀어보았다.

https://gracefulprograming.tistory.com/3

 

[C++] map vs hash_map(unordered_map)

개요 hash_map은 비표준 Container인데 반해(stdext namespace에 포함) unordered_map은 C++11에서 STL 표준 Container로 추가되었으며, (사실 TR1부터 추가되었지만 C++11에서 좀 더 최적화가 이루어졌다고 합..

gracefulprograming.tistory.com

https://alstj-success.tistory.com/84?category=952760 

 

프로그래머스 lv1 [완주하지 못한 선수] C++, python - 해시

문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/42576?language=python3 코딩테스트 연습 - 완주하지 못한 선수 수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고..

alstj-success.tistory.com

 

소스 코드

#include <bits/stdc++.h>
using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";
    //해시 맵 생성    #include <unordered_map> 
    unordered_map <string, int> um;
    
    //참가자 명단의 이름의 키인 맵에 1 추가
    for(string a : participant) {
        um[a]++;
    }
    
    //완주자 명단의 이름이 키인 맵에 1 추가
    for(string b : completion) {
        um[b]--;
    }
    
    //맵의 값(.second)을 확인해 0이 아니면 해당 맵의 키(.first) 반환
    for(auto c : um) {
        if(c.second >0){
            answer = c.first;
            break;
        }
    }
    return answer;
}