Skip to content

Commit 333c451

Browse files
committed
edit code
1 parent a59da3c commit 333c451

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed
+18-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
package Week1.공통.달리기경주;
2-
// 달리기 경주
3-
import java.util.Arrays;
4-
import java.util.HashMap;
1+
import java.util.*;
52

6-
class jiyoon {
7-
public String[] solution(String[] players, String[] callings) {
3+
class Solution {
4+
public String[] solution(String[] players, String[] callings) {
5+
HashMap<String, Integer> rank = new HashMap<>(); // Key : name, Value : rank
86

9-
HashMap<String, Integer> map = new HashMap<>(); // Key : 선수 이름, Value : 등수
7+
for (int i = 0; i < players.length; i++) rank.put(players[i], i);
108

11-
for (int i = 0; i < players.length; i++) map.put(players[i], i);
9+
for (int i = 0; i < callings.length; i++) {
10+
String name = callings[i]; // 추월한 선수
11+
int nowIndex = rank.get(name); // 추월한 선수의 현재 등수
12+
int preIndex = nowIndex - 1; // 추월당한 선수의 현재 등수
1213

13-
for (String s : callings) {
14-
int ahead = map.get(s); // 추월하는 선수
15-
int behind = map.get(s) - 1; // 추월당하는 선수
14+
String preName = players[preIndex]; // 추월당한 선수
1615

17-
String outrun = players[ahead];
18-
String temp = players[behind];
16+
// rank맵 수정
17+
rank.replace(name, preIndex);
18+
rank.replace(preName, nowIndex);
1919

20-
players[behind] = outrun;
21-
players[ahead] = temp;
22-
23-
// 선수들의 등수를 변경
24-
map.put(outrun, behind);
25-
map.put(temp, ahead);
20+
// players배열 수정
21+
players[preIndex] = name;
22+
players[nowIndex] = preName;
2623
}
2724

28-
System.out.println(Arrays.toString(players));
25+
return players;
2926
}
30-
}
27+
}

0 commit comments

Comments
 (0)