-
[프로그래머스] 연습문제 달리기 경주 자바코딩테스트 2025. 3. 28. 12:50반응형
https://school.programmers.co.kr/learn/courses/30/lessons/178871
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
현재 주자 정보인 String[] players를 추월 정보인 String[] callings 에 따라 players를 변경한 뒤 반환.
callings에서 지명될 때 마다 players에서 해당 주자와 그 앞 주자의 순서를 바꾼다.
최초에는 players의 index를
OptionalInt idx = IntStream.range(0, players.length).filter(i -> players[i].equals(call)).findFirst();
방식으로 처리하고자 하였으나, 테스트과정에서 players길이가 길어지니 속도가 저하되었다.
이를 해결하기 위해 Map<String, Integer>로 순위정보를 연동하여 해결하였다.
import java.util.HashMap; import java.util.Map; public class RunningRace { public String[] solution(String[] players, String[] callings) { Map<String, Integer> ranks = new HashMap<>(); for(int i = 0 ; i < players.length; i++){ ranks.put(players[i], i); } for(String call : callings) { int index = ranks.get(call); if (index > 0) { String temp = players[index -1]; players[index -1] = players[index]; players[index] = temp; ranks.put(players[index], index); ranks.put(players[index-1], index-1); } } return players; } }
반응형'코딩테스트' 카테고리의 다른 글
[프로그래머스] 바탕화면 정리 자바 (0) 2025.03.29 [프로그래머스]공원 산책 자바 (0) 2025.03.29 [프로그래머스]PCCE 기출문제[PCCE 기출문제] 10번 / 데이터 분석 자바 (0) 2025.03.28 [프로그래머스] [PCCE 기출문제] 9번 / 이웃한 칸 자바 (0) 2025.03.28 [프로그래머스]붕대감기 자바 (0) 2025.03.27