매일 조금씩

프로그래머스 level1 : 달리기 경주 - Java 본문

알고리즘

프로그래머스 level1 : 달리기 경주 - Java

mezo 2023. 6. 2. 10:50
728x90
반응형

import java.util.*;
import java.util.stream.*;

class Solution {
    
    private static Map<String, Integer> playerInfo = new HashMap<>();
    
    public String[] solution(String[] players, String[] callings) {
        makeInfo(players);
        
        Arrays.stream(callings).forEach(player -> {
            int idx = playerInfo.get(player);
            String frontPlayer = players[idx-1];
            players[idx] = frontPlayer;
            players[idx-1] = player;
            
            playerInfo.put(player, idx-1);
            playerInfo.put(frontPlayer, idx);
        });
        
        return players;
    }
    
    private void makeInfo(String[] players){
        // 인덱스를 함께 뽑아내려고 IntStream을 사용 
        IntStream.range(0, players.length).forEach(index -> playerInfo.put(players[index], index));
    }
}
728x90
반응형