코딩테스트 | python/프로그래머스

[프로그래머스 | python] 달리기 경주, 추억 점수

iemxl 2023. 6. 18. 18:08

#달리기 경주

def solution(players, callings):
    for k in callings:
        ch=players.index(k)
        players[ch-1],players[ch]=players[ch],players[ch-1]
    answer=players
    return answer

>>시간초과

 

def solution(players, callings):
    answer = []
    dic_players = {}
    dic_lank = {}
    
    for idx, i in enumerate(players):
        dic_players[i] = idx
        dic_lank[idx] = i
    
    for i in callings:
        cur_idx = dic_players[i]
        
        dic_players[i] = cur_idx - 1
        dic_players[dic_lank[cur_idx-1]] = cur_idx
        
        dic_lank[cur_idx] = dic_lank[cur_idx - 1]
        dic_lank[cur_idx - 1] = i
    
    answer = list(dic_lank.values())
             
    return answer

 

#추억 점수

def solution(name, yearning, photo):
    answer = [0 for i in range(len(photo))]
    for i in range(len(photo)):
        for j in name:
            answer[i]+=(photo[i].count(j)*yearning[name.index(j)])
    return answer