1. 벡터 풀이
기본 풀이 이중 for문은 시간초과
두 배열의 크기가 1차이 나는 점을 이용해서 정렬 후 같은 인덱스에 다른 값이 있는 경우 participant 값 반환
끝까지 같을 경우 participant 마지막 원소값 반환
2. 해시맵
문제에서 주어진 배열은 순서가 상관없기 때문에 unordered_map이 더 빠름
중복되는 이름이 있을 수 있기에 <string, int> 로 key, value 설정 후 count
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
/*
string answer = "";
sort(participant.begin(), participant.end());
sort(completion.begin(), completion.end());
int pSize = participant.size();
for(int i=0; i<pSize; i++){
if(participant[i]!=completion[i]){
answer = participant[i];
break;
}
}
if(answer == "")
answer = participant[pSize-1];
*/
unordered_map<string, int> um;
for(string p : participant){
um[p]++;
}
for(string c : completion){
um[c]--;
}
for(auto m : um){
if(m.second > 0){
answer = m.first;
break;
}
}
return answer;
}
'알고리즘 > 알고리즘' 카테고리의 다른 글
[C++] 프로그래머스 : 위장 (0) | 2022.01.04 |
---|---|
[C++] 프로그래머스 : 전화번호 목록 (0) | 2022.01.03 |
[C++] 백준 20058번 : 마법사 상어와 파이어스톰 (0) | 2021.10.09 |
[C++] 백준 10159번 : 저울 (0) | 2021.09.10 |
[C++] 백준 1800번 : 인터넷설치 (0) | 2021.09.10 |