[파이썬/Python/프로그래머스] [1차] 추석 트래픽

SooYeon Yeon·2022년 6월 15일
0

파이썬/알고리즘

목록 보기
21/35

코드

def solution(lines):
    answer = 1
    information = []
    ms_times=[]
    tmp = []
    max_cnt = [0]*len(lines)
    s_time=[]
    
    print(max_cnt)
    
    if len(lines)==1:
        return 1
       
    for i in range(len(lines)):
        tmp = lines[i].split()
        ms_times.append(calculateMS(str(tmp[1])))
        s_time.append(float(tmp[2][:-1]))
    
    for i in range(len(ms_times)+1):
        cnt=i+1
        while cnt<len(ms_times):
            if int(calculatePreviousTime(ms_times[cnt],s_time[cnt])) < int(ms_times[i])+1000:
                max_cnt[i]+=1
            cnt+=1
    answer += max(max_cnt)
            
    return answer

def calculateMS(time):
    return ( int(time[0:2])*3600 + int(time[3:5])*60 + int(time[6:8] )) * 1000 + int(time[9:12])

def calculatePreviousTime(s, t):
    return s - t*1000 + 1

풀이 방법

  1. 먼저 날짜는 제외 해도 되므로 그 뒤에 것으로 활용한다.
  2. 한개만 입력했을 시 1이기 때문에 처리를 해주고, answer도 1부터 시작한다.
  3. 응답완료시간(s)와 처리시간 (t)를 따로 리스트를 만들어 저장해주었다.
  4. 응답 완료 시간을 ms 단위로 바꾸어 준다. (계산 편하게 하기 위해) calculateMS 함수
  5. i는 0부터 끝까지 돌리고, cnt는 i+1부터 해서 자신의 뒤에 있는 것들과만 비교한다.
  6. 만약 그 다음값(cnt 해당 값)이 i의 값에 1초를 더한 것 보다 작으면 구간 안에 들어있는 것이기 때문에 max_cnt[i]를 증가시키고 이어서 진행한다.
  7. 마지막에 max_cnt중 가장 큰 값을 더해준다.

0개의 댓글