반응형
문제
https://school.programmers.co.kr/learn/courses/30/lessons/178871
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
🐍파이썬
더보기
시간초과 코드
def solution(players, callings):
for i in callings:
a = players.index(i)
temp = players[a-1]
players[a-1] = i
players[a] = temp
return players
def solution(players, callings):
for i in callings:
a = players.index(i)
players.remove(i)
players.insert(a-1, i)
return players
def solution(players, callings):
dic = {player:idx+1 for idx, player in enumerate(players)} #각 등수 선수의 현재 인덱스 값
dic2 = {idx+1:player for idx, player in enumerate(players)} #인덱스 값에 대한 선수 등수
for i in callings:
a = dic[i] #이름을 불린 선수의 현재 등수
b = dic2[a-1] #이름을 불린 선수의 전 등수의 선수
dic[b] = a #전 등수 선수의 등수+1(밀려남)
dic[i] = a-1 #이름을 불린 선수의 등수-1(이전 선수 추월)
dic2[a] = b #이름을 불린 선수의 등수에는 전 등수의 선수를 대입
dic2[a-1] = i #전 선수의 등수에는 이름을 불린 선수를 대입
return list(dic2.values())
풀이를 참고한 블로그
[프로그래머스 파이썬] 달리기 경주
얀에서는 매년 달리기 경주가 열립니다. 해설진들은 선수들이 자기 바로 앞의 선수를 추월할 때 추월한 선수의 이름을 부릅니다. 예를 들어 1등부터 3등까지 "mumu", "soe", "poe" 선수들이 순서대로
velog.io
다른 풀이 방법
# def solution(players, callings):
# for j in callings:
# num = players.index(j)
# players[num-1], players[num] = players[num], players[num-1]
# return players
def solution(players, callings):
player_indices = {player: index for index, player in enumerate(players)}
for j in callings:
current_index = player_indices[j]
desired_index = current_index - 1
if current_index > 0 and players[desired_index] != j:
players[current_index], players[desired_index] = players[desired_index], players[current_index]
player_indices[players[current_index]] = current_index
player_indices[players[desired_index]] = desired_index
return players
반응형
'Problem Solving > 프로그래머스' 카테고리의 다른 글
[프로그래머스|파이썬] 뒤에 있는 큰 수 찾기 (연습문제/lv.2) (0) | 2023.05.22 |
---|---|
[프로그래머스|파이썬] 베스트앨범 (해시/level 3) (0) | 2023.04.29 |
[프로그래머스|파이썬] 공원 산책 (연습문제/level 1) (0) | 2023.04.28 |
[프로그래머스 | 파이썬 / 자바스크립트] 게임 맵 최단거리(깊이/너비 우선 탐색(DFS/BFS)/level 2) (0) | 2023.04.10 |
[프로그래머스 | 파이썬 / 자바스크립트] 타겟 넘버(깊이/너비 우선 탐색(DFS/BFS)/level 2) (0) | 2023.04.02 |
[프로그래머스 | 파이썬 / 자바스크립트] 스킬트리(Summer/Winter Coding(~2018) / level 2) (0) | 2023.04.02 |