공부/C++
[C++] 프로그래머스 완주하지 못한 선수
굴러다니다니
2025. 4. 4. 01:37
*해시맵을 사용하자 (해시맵 = unordered_map)
#include <unordered_map>
unordered_map<string, int> count_map; //(string, int) 쌍을 가지는 count_map 이름의 해시맵 선언
참가자 이름을 세서 count_map에 넣고, 완주자의 이름이 있으면 개수 하나씩 빼기.
(string, int)쌍의 pair에서 pair.second인 int값이 0보다 큰 이름을 반환
#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
unordered_map<string, int> count_map;
for (string name : participant) {
count_map[name]++;
}
for (string name : completion) {
count_map[name]--;
}
for (auto pair : count_map) {
if (pair.second > 0) {
return pair.first;
}
}
return "";
}
https://school.programmers.co.kr/learn/courses/30/lessons/42576
728x90
반응형