본문 바로가기
Algorithm/프로그래머스

[Algorithm / Programmers] 달리기 경주

by newtownboy 2024. 4. 3.


[Version]
⦁ 2024.04.03 / [Algorithm / Programmers] 달리기 경주

 

import java.util.*;

class Solution {
    public String[] solution(String[] players, String[] callings) {
        Map<String, Integer> playerIndices = new HashMap<>();
        for (int i = 0; i < players.length; i++) {
            playerIndices.put(players[i], i);
        }
        
        for (String calling : callings) {
            int index = playerIndices.get(calling);
            String tempPlayer = players[index - 1];
            players[index - 1] = players[index];
            players[index] = tempPlayer;
            
            playerIndices.put(players[index - 1], index - 1);
            playerIndices.put(players[index], index);
        }
        
        return players;
    }
}