728x90
https://school.programmers.co.kr/learn/courses/30/lessons/178871
์ฒ์์ 2์ค ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ๋๋ ์๊ฐ์ด๊ณผ๊ฐ ๋จ๊ธธ๋,
ํด์๋งต์ ์ฌ์ฉํด์ผ ํ๋ ๊ฒ์ ๊นจ๋ฌ์๋ค
Map์ 2๊ฐ ์ฐ๋ ๋ฐฉ์ ๋ฑ ํ์ด๊ฐ ๋ค์ํ๋๋ฐ, ๋๋ unordered map ํ๋๋ก players์ ์ธ๋ฑ์ค๋ฅผ ๋ฐ๋ก ๊ฒ์ํด์ ํด๊ฒฐํจ
๋์ ํ์ด
#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> m; // key=์ ์์ด๋ฆ, value=๋ฑ์
int len = players.size();
for(int p=0; p<len; ++p){
m[players[p]] = p;
}
for(int i=0; i<callings.size(); ++i){
int cnt = m[callings[i]];
string tmp = players[cnt-1]; // players ์ธ๋ฑ์ค ๋ฐ๋ก ์ฐพ์์ ๋ณ๊ฒฝ
players[cnt-1] = players[cnt];
players[cnt] = tmp;
m[callings[i]] -= 1;
m[tmp] += 1;
}
return players;
}
728x90
'๐ ์๊ณ ๋ฆฌ์ฆ > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++/PGS] Lv.3 : ์ฌ ์ฐ๊ฒฐํ๊ธฐ (๊ทธ๋ฆฌ๋ Greedy) (0) | 2024.11.01 |
---|---|
[MySQL/PGS] Lv.1 : ์๋์ฐจ ๋์ฌ ๊ธฐ๋ก์์ ์ฅ๊ธฐ/๋จ๊ธฐ ๋์ฌ ๊ตฌ๋ถํ๊ธฐ (0) | 2023.09.19 |
[C++/PGS] Lv.2 : ๋ ๋งต๊ฒ (ํ Heap) (1) | 2023.09.19 |
[C++/PGS] Lv.3 : ์ด์ค์ฐ์ ์์ํ (0) | 2023.09.17 |
[C++/PGS] Lv.2 : ์ด์ง ๋ณํ ๋ฐ๋ณตํ๊ธฐ (0) | 2023.09.17 |