- 난이도: Lv1
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/178871
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/1/178871. 달리기 경주
풀이 시간 : 20분
import java.util.*;
class Solution {
public String[] solution(String[] players, String[] callings) {
String[] answer = players;
List<String> answerArr = Arrays.asList(answer);
for(int i=0; i<callings.length; i++){
int rank = answerArr.indexOf(callings[i]);
String changeName = answer[rank-1];
answer[rank-1] = answer[rank];
answer[rank] = changeName;
}
return answer;
}
}
//before
List<String> answerArr = Arrays.asList(answer);
//after
Map<String, Integer> answerMap = new HashMap<>();
for(int i=0; i<players.length; i++){
answerMap.put(players[i], i);
answerMap.put(answer[rank-1], rank-1);
answerMap.put(answer[rank], rank);
}
풀이 시간 : 30분(첫 풀이 시간 포함)
import java.util.*;
class Solution {
public String[] solution(String[] players, String[] callings) {
String[] answer = players;
Map<String, Integer> answerMap = new HashMap<>();
for(int i=0; i<players.length; i++){
answerMap.put(players[i], i);
}
for(int i=0; i<callings.length; i++){
int rank = answerMap.get(callings[i]);
String changeName = answer[rank-1];
answer[rank-1] = answer[rank];
answerMap.put(answer[rank-1], rank-1);
answer[rank] = changeName;
answerMap.put(answer[rank], rank);
}
return answer;
}
}
풀이 시간 : 35분(첫 풀이 시간 포함)
import java.util.*;
class Solution {
public String[] solution(String[] players, String[] callings) {
String[] answer = players;
Map<String, Integer> answerMap = new HashMap<>();
for(int i=0; i<players.length; i++){
answerMap.put(players[i], i);
}
//for(int i=0; i<callings.length; i++){
for(String player : callings){
//int rank = answerMap.get(callings[i]);
int rank = answerMap.get(player);
String changeName = answer[rank-1];
answer[rank-1] = answer[rank];
answerMap.put(answer[rank-1], rank-1);
answer[rank] = changeName;
answerMap.put(answer[rank], rank);
}
return answer;
}
}