[JAVA 문제 풀이] 323. 달리기 경주

프로그래머스 (178871)
Stupefyee's avatar
Jun 17, 2025
[JAVA 문제 풀이] 323. 달리기 경주
notion image
 

내가 작성한 코드

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 calling : callings) { int idx = idxMap.get(calling); // 현재 위치 String front = players[idx - 1]; // 앞자리 선수 이름 players[idx - 1] = calling; // 현재 선수 이름을 앞자리로 이동 players[idx] = front; // 앞자리 선수를 현재 선수 위치로 이동 // Map의 인덱스도 같이 갱신 idxMap.put(calling, idx - 1); idxMap.put(front, idx); } return players; } }
Share article

stupefyee