입출력 예시
today | terms | privacies | result |
"2022.05.19" | ["A 6", "B 12", "C 3"] | ["2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C"] | [1, 3] |
"2020.01.01" | ["Z 3", "D 5"] | ["2019.01.01 D", "2019.11.15 Z", "2019.08.02 D", "2019.07.01 D", "2018.12.28 Z"] | [1, 4, 5] |
나의 코드
def solution(today, terms, privacies):
answer = []
term_dict = dict(zip([terms[i][0] for i in range(len(terms))], [int(terms[i][2:]) for i in range(len(terms))]))
today = int(today.replace('.', ''))
print(today)
for i, x in enumerate(privacies):
date, term = x.split()
y, m, d = map(int, date.split('.'))
rem = (m + term_dict[term]) % 12
if rem == 0:
y = y + (m + term_dict[term]) // 12 - 1
m = 12
else:
y = y + (m+term_dict[term]) // 12
m = rem
m = str(m).zfill(2)
d = str(d).zfill(2)
end_date = int(f'{y}{m}{d}')
if today >= end_date:
print(end_date)
answer.append(i+1)
return answer
Python
복사
다른 풀이
def to_days(date):
year, month, day = map(int, date.split("."))
return year * 28 * 12 + month * 28 + day
def solution(today, terms, privacies):
months = {v[0]: int(v[2:]) * 28 for v in terms}
today = to_days(today)
expire = [
i + 1 for i, privacy in enumerate(privacies)
if to_days(privacy[:-2]) + months[privacy[-1]] <= today
]
return expire
Python
복사
개선점 분석
•
날짜(date)를 기준으로 대소 관계를 비교하기 보다, 지난 일수(days)를 가지고 비교하는게 더 쉽다.