카테고리 없음

[Programmers, Level 1] Racing

xeohyuni 2023. 5. 20. 20:24

Description

Running races are held every year in Jan. Commentators call the name of the player who overtook them when they overtake the player right in front of them. For example, when the first to third-place "mumu," "soe," and "poe" players were running in order, the second-place "soe" player overtook the first-place "mumu" player if the commentators called the "soe." This means that the "soe" player will take first place and the "mumu" player will take second place.

When the parameters are given a string array players with the names of the players in the order of first place to the current rank and a string array calls with the names of the commentators, complete the solution function that returns the players in the order of first place to the rank.

 

Resctrictions

  • 5 ≤ players length ≤ 50,000
  • Players[i] means the name of the i-th player.
  • The elements of players are made up of only lowercase letters.
  • Players does not contain duplicate values.
  • 3 ≤ players[i] length ≤ 10
  • Length of 2 ≤ calls ≤ 1,000,000
  • Calls are made up of only the elements of players.
  • The name of the first runner in the race is not called.
players callings result
["mumu", "soe", "poe", "kai", "mine"] ["kai", "kai", "mine", "mine"] ["mumu", "kai", "mine", "soe", "poe"]

Input/Output Example #1
The fourth-place "kai" will overtake the runner-up twice, while the previous third-place "poe" and second-place "soe" will be fourth and third-place. The fifth-place "mine" competitor overtakes the fourth place twice, and the third-place "poe" and "soe" competitors finish fifth and fourth, and the race ends. ["mumu", "kai", "min", "soe", "poe"] are placed in the first place array.

def solution(players, callings):
    players_dict = {}
    #player's dic 
    # key = player 
    # value  = 등수
    for i,player in enumerate(players):
        players_dict[player] = i
        
    idx_dict = {}
    #idx's dic
    # key = 등수
    # value = player
    for i,player in enumerate(players):
        idx_dict[i] = player
        
        
    for name in callings:
        idx = players_dict[name]
        front_idx = idx-1
        
        front_player = idx_dict[front_idx]
        
        players_dict[name],players_dict[front_player] = front_idx, idx
        
        idx_dict[idx],idx_dict[front_idx] = front_player, name
    return list(idx_dict.values())

Discussion:

The player_dict determines the key of the player's name and the value of the player's current rank. The idx_dict is the opposite of player_dict's key and value. It says that the key is the current rank of the player in index and the value is the player's name order in index. 

The reason why I made player_dict and idx_dict is to view the order(rank) and player's name easily. We could find the player's name through rank or find the rank through the player's name.

At here:

    for i,player in enumerate(players):
        players_dict[player] = i

- Evrytime the for functions, it brings all the key and the index together by using enumerate one by one.

1) mumu's index = 0

2) soe's index = 1

                  .

                  .

                  .

At here:

    for i,player in enumerate(players):
        idx_dict[i] = player

- it is just the opposite way from the previous explanation. 

 

At here: we need to concentrate the player who is going to overstrip and the player who is going to be overstripped.

    for name in callings:
        idx = players_dict[name]
        front_idx = idx-1 # finding the player who will be overstripped
        # If calling is kai, kai's surrent rank is number 3 and then he will be ranked as number 2. Before kai will overstrip, we need to know the location where the plater is going to be overstripped, which is poe.
        front_player = idx_dict[front_idx]
        
        players_dict[name],players_dict[front_player] = front_idx, idx
        #poe's rank and kai's rank will change 
        idx_dict[idx],idx_dict[front_idx] = front_player, name

        #also the name's location too.

 

 

The end