Level 1. 달리기 경주
매번 인덱스를 찾을 필요 없이, 해시 맵에 인덱스를 저장하고 정렬 상태를 유지할 필요가 없으므로 unordered_map을 사용한다.
플레이어 이름을 바꿀 때, 인덱스도 같이 바꿔줘야 함에 주의한다.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
vector<string> solution(vector<string> players, vector<string> callings)
{
unordered_map<string, int> umap;
for (int i = 0; i < players.size(); ++i)
umap[players[i]] = i;
for (string calling : callings)
{
string& p1 = players[umap[calling] - 1];
string& p2 = players[umap[calling]];
swap(umap[p1], umap[p2]);
swap(p1, p2);
}
return players;
}
'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글
Level 1. 시저 암호 (0) | 2023.04.19 |
---|---|
Level 1. 공원 산책 (0) | 2023.04.18 |
Level 2. [3차] 파일명 정렬 (0) | 2023.04.16 |
Level 2. 괄호 회전하기 (0) | 2023.04.15 |
Level 2. 가장 큰 수 (0) | 2023.04.14 |