pooney

처음 Map을 사용하지 않고 for문으로 해결한 경우 최악의 수가 나와 시간에러가 발생하였다 이후 시간 문제를 해결 하기 위해 Map을 사용하는 것으로 해결하였다.

 

import java.util.*;
class Solution {
    public String[] solution(String[] players, String[] callings) {
        Map<String,Integer> idxMap = new HashMap<>();
        for(int i=0; i<players.length; i++){
            idxMap.put(players[i],i);
        }


        for(String call : callings){
            Integer idx = idxMap.get(call);
            players[idx]  = players[idx-1];
            players[idx-1] = call;
            idxMap.put(call,idx-1);
            idxMap.put(players[idx], idx);
        }

        return players;
    }
}

 

 

 

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

profile

pooney

@pooney

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!