알고리즘 2023kakaoblind-개인정보 수집 유효기간(python)

2023. 4. 30. 01:45·알고리즘

def solution(today, terms, privacies):
    termDict = {}
    answer =[]
    todayyear, todaymonth, todayday = int(today[0:4]), int(today[5:7]), int(today[8:])
    for i in terms:
        termDict[i[0]] = int(i[2:])
    for index, j in enumerate(privacies):
        uhogigan = termDict[j[-1]]
        year = int(j[0:4])
        month = int(j[5:7])
        day = int(j[8:10])
        aftermonth = month + int(uhogigan)
        if(aftermonth>12):
            year+=1
            aftermonth -= 12
        if(year>todayyear): #이미 유효기간이 개인정보년보다 길면
            continue
        elif(year==todayyear):
            if(aftermonth>todaymonth):
                continue
            elif(aftermonth == todaymonth):
                if(todayday<day):
                    continue
        answer.append(index+1)
    return answer

첫번째 트라이. 주어진 예시들은 다 통과지만 테스트 결과 30밖에 안나왔다. 일단 틀린 풀이지만 천천히 설명을 해보자면 terms에 ["A 9","B 4"]이런식으로 주어지면 이것을 {"A": 9, "B" : 4} 딕셔너리 형식으로 바꿨다. 후에 privacies마지막 char이 알파벳이 될텐데 거기서 뽑아서 바로 키값에 넣어 value를 찾기 위해서이다. 유효기간이 넘는것의 인덱스넘버+1을 출력하는게 목표기에 enumerate로 배열을 받아왔다. 그 후 이전에만든 dict의 키로 우리가 원하는 기간을 꺼낸뒤 그것을 privacies의 month와 더해준다. 
만약 그것이 12월을 넘은경우는 연도에 +1을 해주고 aftermonth는 12를 빼준다.

그 후 이후연도와 오늘연도를 비교해 만약 작다면 추가해야하고 아니라면 이후월과 오늘월을 비교해 작다면 추가하고 아니라면 이후일과오늘일을 비교해 작다면 추가하기 위해 그 외의것들을 continue로 해주고 위에 말한경우만 append해주었다. 

에러가 뜨는 이유를 한참동안 수정하다가 반례를 찾아버렷다. 바로 if(aftermonth>12)여기인데

today = "2024.4.12", terms= ["F 24"]가 나왔고 privacies가 "2022.12.12 F"라고 해보자 이렇게 되면 aftermonth는 36이되는데 if문 한번돌면 24가 되고 년도는 2023이 되기에 추가되지 않는다. 그렇기에 while문으로 돌려주어야 우리가 원하는 값을 내보낼 수 있다. 

왜 놓쳤지..?최종코드

def solution(today, terms, privacies):
    termDict = {}
    answer =[]
    todayyear, todaymonth, todayday = int(today[0:4]), int(today[5:7]), int(today[8:])
    for i in terms:
        termDict[i[0]] = int(i[2:])
    for index, j in enumerate(privacies):
        uhogigan = termDict[j[-1]] 
        year,month,day = int(j[0:4]), int(j[5:7]),int(j[8:10]) 
        aftermonth = month + uhogigan
        while(aftermonth>12):
            year+=1
            aftermonth -= 12
        if(year>todayyear): #이미 유효기간이 개인정보년보다 길면
            continue
        elif(year==todayyear):
            if(aftermonth>todaymonth):
                continue
            elif(aftermonth == todaymonth):
                if(todayday<day):
                    continue
        answer.append(index+1)
    return answer

이건 시간복잡도가 n이기에 시간초과 이슈는 없었다.

'알고리즘' 카테고리의 다른 글

1043-python  (1) 2023.05.11
[프로그래머스]-이모티콘할인행사(swift)  (1) 2023.05.04
프로그래머스-귤고르기(Python)  (0) 2023.04.29
1647 - swift  (1) 2023.04.24
1987-python & swift  (1) 2023.04.24
'알고리즘' 카테고리의 다른 글
  • 1043-python
  • [프로그래머스]-이모티콘할인행사(swift)
  • 프로그래머스-귤고르기(Python)
  • 1647 - swift
2료일
2료일
좌충우돌 모든것을 다 정리하려고 노력하는 J가 되려고 하는 세미개발자의 블로그입니다. 편하게 보고 가세요
  • 2료일
    GPT에게서 살아남기
    2료일
  • 전체
    오늘
    어제
    • 분류 전체보기 (120)
      • SWIFT개발 (29)
      • 알고리즘 (25)
      • Design (6)
      • ARkit (1)
      • 면접준비 (30)
      • UIkit (2)
      • Vapor-Server with swift (3)
      • 디자인패턴 (5)
      • 반응형프로그래밍 (12)
      • CS (3)
      • 도서관 (1)
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
2료일
알고리즘 2023kakaoblind-개인정보 수집 유효기간(python)
상단으로

티스토리툴바