알고리즘

프로그래머스 달리기 경주 (JAVA)

박카스마시며코딩 2023. 4. 20. 19:56

https://school.programmers.co.kr/learn/courses/30/lessons/178871/

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

import java.util.*;
class Solution {
    public String[] solution(String[] players, String[] callings) {
        int size = players.length;
        Map<String,Integer> findIndex = new HashMap<>();
        for(int i = 0 ; i < size ; i++){
            findIndex.put(players[i],i);
        }
        for(String call : callings){
            int nowIndex = findIndex.get(call);
            int prevIndex = findIndex.get(players[nowIndex-1]);
            String temp = players[prevIndex];
            players[prevIndex] = players[nowIndex];
            players[nowIndex] = temp;
            findIndex.put(players[prevIndex],prevIndex);
            findIndex.put(players[nowIndex],nowIndex);
        }
        String[] answer = players;
        return answer;
    }
}