Notice
Recent Posts
Recent Comments
Link
250x250
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

거인의 코딩일지

[Lv.1_Java] 달리기 경주 본문

알고리즘테스트/Programmers

[Lv.1_Java] 달리기 경주

코딩거인 2023. 7. 13. 14:05
728x90

import java.util.Map;
import java.util.HashMap;

class Solution {
    public String[] solution(String[] players, String[] callings) {
        Map<String, Integer> map = new HashMap<>();
        
        for(int i =0; i< players.length; i++){
            map.put(players[i], i);
        }
        
        for(String player : callings){
            // 등수와 앞에 가던 선수를 가져옴
            int num = map.get(player);
            String overPlayer = players[num-1];
            
            // players 의 배열을 update 해준다음
            players[num-1] = player;
            players[num] = overPlayer;
			// map에 담아주었다.            
            map.put(player, num-1);
            map.put(overPlayer, num);
        }
        return players;
    }
}

 

배열을 써서 하기에는 더 복잡할 것같아서 map 을 생성해서 담아준뒤 for 문으로 계산식 만들어서 해결했다!!

728x90