프로그래머스 달리기 경주 문제이다
def solution(players, callings):
for name in callings:
idx = players.index(name) # .index() 함수가 시간복잡도면에서 비효율적인가보다
players[idx], players[idx-1] = players[idx-1], players[idx]
return players
쉽다고 생각하고 작성하니 시간 초과가 나타난다
때문에 .index() 부분을 딕셔너리를 이용해 index를 구하는 방식으로 하여 해결했다.
def solution(players, callings):
diction = {}
for i in range(len(players)):
diction[players[i]] = i
for name in callings:
idx = diction[name]
temp = players[idx-1]
players[idx], players[idx-1] = players[idx-1], players[idx]
diction[name],diction[temp] = idx-1 , idx
return players