경주가 끝난 후 선수들의 이름을 1등부터 등수 순서대로 배열에 담아 return
| players | callings | result |
|---|---|---|
| ["mumu", "soe", "poe", "kai", "mine"] | ["kai", "kai", "mine", "mine"] | ["mumu", "kai", "mine", "soe", "poe"] |
players의 길이 ≤ 50,000players[i]는 i번째 선수의 이름을 의미합니다.players의 원소들은 알파벳 소문자로만 이루어져 있습니다.players에는 중복된 값이 들어가 있지 않습니다.players[i]의 길이 ≤ 10callings의 길이 ≤ 1,000,000callings는 players의 원소들로만 이루어져 있습니다.
- map사용
- map은 인덱스 위치를 직접 변경하는 것이 아니라 value로 순위표시
- 선수들 실제 인덱스 변경은 매개변수의 players[] 에서!
=> 아래 전체코드를 설명, 주석과 함께 보면 이해가 잘될 것이다.
import java.util.*;
class Solution {
public String[] solution(String[] players, String[] callings) {
Map<String, Integer> rank = new HashMap<>();
for(int i=0; i<players.length; i++)
rank.put(players[i],i);
for(String player : callings) {
int playerRank = rank.get(player); //선수의 현재 순위
String front = players[playerRank-1]; //앞 선수 이름
rank.replace(front, playerRank);//앞 선수의 위치 변경
players[playerRank] = front;
rank.replace(player, playerRank-1) ; //현재 선수의 순위를 앞으로 변경
players[playerRank-1] = player;
}
return players;
}
}