1
- package Week1 .공통 .달리기경주 ;
2
- // 달리기 경주
3
- import java .util .Arrays ;
4
- import java .util .HashMap ;
1
+ import java .util .*;
5
2
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
8
6
9
- HashMap < String , Integer > map = new HashMap <>(); // Key : 선수 이름, Value : 등수
7
+ for ( int i = 0 ; i < players . length ; i ++) rank . put ( players [ i ], i );
10
8
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 ; // 추월당한 선수의 현재 등수
12
13
13
- for (String s : callings ) {
14
- int ahead = map .get (s ); // 추월하는 선수
15
- int behind = map .get (s ) - 1 ; // 추월당하는 선수
14
+ String preName = players [preIndex ]; // 추월당한 선수
16
15
17
- String outrun = players [ahead ];
18
- String temp = players [behind ];
16
+ // rank맵 수정
17
+ rank .replace (name , preIndex );
18
+ rank .replace (preName , nowIndex );
19
19
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 ;
26
23
}
27
24
28
- System . out . println ( Arrays . toString ( players )) ;
25
+ return players ;
29
26
}
30
- }
27
+ }
0 commit comments