
https://school.programmers.co.kr/learn/courses/30/lessons/178871
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
간단한 자료구조 문제입니다.
callings에 들어있는 이름을 통해 player를 찾아 swap해줘야합니다.
그렇다면 바로 map이 생각나게됩니다.
순서가 상관없기 때문에 unordered_map을 사용하게 되며,
map은 key로는 name, value로는 인덱스(혹은 진짜 주소)를 가질 것입니다.
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
vector<string> solution(vector<string> players, vector<string> callings) {
vector<string> answer;
unordered_map<string, int> player_map;
int idx = 0;
for (string& player : players)
{
player_map[player] = idx;
++idx;
}
for (string& calling : callings)
{
int cur = player_map[calling];
if (cur > 0)
{
swap(player_map[players[cur - 1]], player_map[calling]);
swap(players[cur], players[cur - 1]);
}
}
return players;
}
'Algorithm > PS' 카테고리의 다른 글
| [1일 1알고] 숫자 타자 대회 (0) | 2026.06.24 |
|---|---|
| [1일 1알고] 등산코스 정하기 (0) | 2026.06.22 |
| [1일 1알고] 물 부족 (0) | 2026.06.16 |
| [1일 1알고] 호텔 방 배정 (0) | 2026.06.12 |
| [1일 1알고] 기지국 설치 (0) | 2026.06.11 |