CodingTest/Programmers Lv.1
[프로그래머스 C++] Lv.1 완주하지 못한 선수
주녘
2024. 7. 30. 11:03
https://school.programmers.co.kr/learn/courses/30/lessons/42576
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 정답코드 및 핵심 아이디어, 유의사항
- completion의 길이는 participant의 길이보다 1 작음
- 동명이인이 있을 수 있음
주석 참조
#include <string>
#include <vector>
#include <map>
using namespace std;
string solution(vector<string> participant, vector<string> completion)
{
string answer;
map<string, int> mapset;
for (auto elem : completion)
{
if (mapset.end() == mapset.find(elem))
mapset.insert({ elem, 1 });
else
mapset[elem]++;
}
for (auto elem : participant)
{
if (mapset.end() == mapset.find(elem))
return elem;
else
{
mapset[elem]--;
if (mapset[elem] < 0)
return elem;
}
}
return NULL;
}
+ 다른 풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(vector<string> participant, vector<string> completion)
{
sort(participant.begin(), participant.end());
sort(completion.begin(), completion.end());
for (int i = 0; i < completion.size(); i++)
if (participant[i] != completion[i])
return participant[i];
return participant[participant.size() - 1];
}