*내가 작성한 코드
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() 를 활용하자! 딕셔너리 !!!!!!!
'알고리즘 > 구현' 카테고리의 다른 글
특정 키를 기준으로 배열 오름차순 정렬하기(lambda) - python (0) | 2024.01.22 |
---|---|
파이썬 enumerate() 내장함수 사용 (0) | 2024.01.12 |
프로그래머스 - 대충 만든 자판(Python) (0) | 2024.01.08 |
lambda를 활용한 sort (Python) (1) | 2024.01.02 |
시간 복잡도를 줄이기 위한 소수 구하기, 약수 구하기 (Python) (0) | 2023.12.31 |