(진행중)[codeup] 공주님의 정원

데린이·2022년 5월 18일
0

날짜 데이터 비교하기
https://codeup.kr/problem.php?id=4713&rid=0

22-05-18

def date_to_num(date): # month date -> int(month+day)   (e.g. 1 1 -> 101)
    start = int(date[0]+'0'+date[1]) if len(date[1]) == 1 else int(date[0]+date[1])
    end = int(date[2]+'0'+date[3]) if len(date[3]) == 1 else int(date[2]+date[3])
    
    return start,end

# input
N = int(input())
date_list = []
for _ in range(N):
    num = date_to_num(input().split())
    date_list.append(tuple(num if num[1] > 301 else [1231,1231]))
date_list = sorted(list(set(date_list)))

# cnt : # flowers, start : the first date where another flower is needed, end: the last date of the flower
cnt, start,end = 0, 301,0
condition = True

while condition: # while stops when we have avaliable flowers from 301~1130 or there is no available flower anymore.
    # fl_list : list of flowers blooming before 'start'
    fl_list = []
    for i,dl in enumerate(date_list):
        if (dl[0] <= min(start,1130)):
            fl_list.append(dl)
            end = max(end,dl[1]) # we finally select the flower which will bloom the longest
        else:
            i -= 1
            break
            
    # pick the flower
    if fl_list:
        cnt += 1
        date_list = [] if i == len(date_list) -1 else date_list[i+1:]
        start = end 
        
        if start > 1130:
            print(cnt)
            condition = False
    else:
        print(0)
        condition = False
  1. 날짜를 숫자로 바꿔서 비교
  2. 개화 시기 오름차순으로 정렬한 후, 개화 시기를 이용해 심을 수 있는 꽃 리스트 산출
    2-1. 조건에 맞지 않는 경우를 발견하면 for문 중단하여 time cost를 줄임.
  3. 2번에서 산출된 리스트에서 꽃이 지는 시기가 가장 늦은 꽃을 선택한다
  • Time : 1660ms
  • Memory : 41536kb

Time cost를 줄여보자!
1. cnt 사용하지 말 것
1-1. lend(fl_list)으로 출력
2. for i in range(x, N)를 사용해 볼 것

profile
취뽀를 기원하는 취준생입니다!

0개의 댓글