⭐아이디어
배열에서 매번 해당 맴버의 번호를 찾으면 너무 느림
1. 미리 map에 해당 맴버의 등수를 저장해둔다
2. callings를 분기한다
3. 해당 멤버의 위치를 map에서 찾는다
4. 해당 멤버의 앞에 위치한 멤버의 위치도 map에서 찾는다.
5. 배열에서 서로의 위치를 변경시킨다
6. map에 저장된 위치값도 서로 바꾼다.
💡풀이 1
import java.util.HashMap;
import java.util.Map;
class Solution {
public String[] solution(String[] players, String[] callings) {
Map<String, Integer> rank = new HashMap<>();
for(int i = 0; i < players.length; i++) {
rank.put(players[i], i);
}
for(String calling : callings) {
int index = rank.get(calling);
String player = players[index - 1];
players[index - 1] = calling;
players[index] = player;
rank.put(calling, index - 1);
rank.put(player, index);
}
return players;
}
}
GitHub - okjunghyeon/Programmers_CodingTest: 프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다.
프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다. Contribute to okjunghyeon/Programmers_CodingTest development by creating an account on GitHub.
github.com
'CodingTest > Programmers' 카테고리의 다른 글
[프로그래머스, Java] 유연근무제 (0) | 2025.07.29 |
---|---|
[프로그래머스, Java] 개인정보 수집 유효기간 (0) | 2025.07.29 |
[프로그래머스, Java] 바탕화면 정리 (0) | 2025.07.29 |
[프로그래머스, Java] 신규 아이디 추천 (0) | 2025.07.29 |
[프로그래머스, Java] 성격 유형 검사하기 (0) | 2025.07.21 |