알고리즘/구현
[프로그래머스] 달리기 경주 - dict() (Python)
mimi04
2024. 1. 28. 13:38
*내가 작성한 코드
def solution(today, terms, privacies):
answer = []
today_year = int(today[:4])
today_month = int(today[5:7])
today_day = int(today[-2:])
term_x = []
for i in range(len(terms)):
term_x.append(terms[i].split())
privacie_x = []
for i in range(len(privacies)):
privacie_x.append(privacies[i].split())
for index, i in enumerate(privacie_x):
m = 0
privacies_year = 0
privacies_month = 0
privacies_day = 0
for j in term_x:
if i[1] == j[0]:
m = int(i[0][5:7]) + int(j[1])
privacies_year = int(i[0][:4]) + (m // 12)
privacies_month = m % 12
privacies_day = int(i[0][-2:])
if today_year == privacies_year and today_month == privacies_month and today_day == privacies_day:
answer.append(index + 1)
else:
if today_year > privacies_year:
answer.append(index + 1)
elif today_year == privacies_year:
if today_month > privacies_month:
answer.append(index + 1)
elif today_month == privacies_month:
if today_day > privacies_day:
answer.append(index + 1)
return answer
print(solution("2021.12.08", ["A 18"], ["2020.06.08 A"]))
결과 -> 1번, 17번 시간 초과
*정답 코드
def solution(players, callings):
play_dict = dict()
for index, name in enumerate(players):
play_dict[name] = index
for player in callings:
idx = play_dict[player]
front = players[idx - 1]
play_dict[player] = idx - 1
play_dict[front] = idx
players[idx - 1] = player
players[idx] = front
return players
*깨달은 점: 시간 절약에는 dict() 를 활용하자! 딕셔너리 !!!!!!!
반응형