문제는 아래와 같습니다.

https://programmers.co.kr/learn/courses/30/lessons/17676

 

코딩테스트 연습 - [1차] 추석 트래픽

입력: [ "2016-09-15 20:59:57.421 0.351s", "2016-09-15 20:59:58.233 1.181s", "2016-09-15 20:59:58.299 0.8s", "2016-09-15 20:59:58.688 1.041s", "2016-09-15 20:59:59.591 1.412s", "2016-09-15 21:00:00.464 1.466s", "2016-09-15 21:00:00.741 1.581s", "2016-09-1

programmers.co.kr

풀이

def solution(lines):

    from datetime import datetime
    temp_1=[]
    answer=0
	
    #1초라는 범위 안에서 겹치는 과정이 몇 개인지 찾는 함수이다.
    def func(log, start, end): 
        cnt = 0 
        for x in log: 
            if x[0] < end and x[1] >= start: 
                cnt += 1 
        return cnt

    for e in lines:
        a=datetime.strptime(' '.join(e.split(' ')[0:2]), '%Y-%m-%d %H:%M:%S.%f') 
        #문자열을 날짜 및 시간 형태로 변환(1)
        
        b=datetime.strptime('2016-09-15 00:00:00.000', '%Y-%m-%d %H:%M:%S.%f') 
        #문자열을 날짜 및 시간 형태로 변환(2) - 기준 시간[원점]
        
        c=int((a-b).total_seconds() * 1000) 
        # a시간과 b시간의 차이를 '초'로 구한다. 밀리초로 변환하기 위해 1000을 곱한다.float 형태 -> integer 형태, 이것은 끝나는 시간이다
        
        d=c-int(float(e.split(' ')[2][:-1])*1000)+1 
        #시작 시간을 구하기 위해 c에서 그만큼을 뺀다. 
        
        temp_1.append([d,c]) 
        #[[시작시간1, 끝나는시간1],[시작시간2, 끝나는시간2], [시작시간3, 끝나는 시간3]] 식으로 정리된다.

    for x in temp_1: 
        answer = max(answer, func(temp_1, x[0], x[0] + 1000), func(temp_1, x[1], x[1] + 1000))  
        
    return answer
 

+ Recent posts