본문 바로가기

알고리즘/프로그래머스

[프로그래머스] [1차] 추석 트래픽

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/17676?language=python3

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

🤔 문제분석

문제요약

9월 15일의 밀리초 단위의 로그가 주어졌을때 1초 동안 트래픽이 제일 많이 걸리는 횟수를 구하는 문제이다.

필요한 개념

  • 완전탐색 : 모든 구간을 다 탐색해본다.
  • 문자열 파싱 : 밀리초 단위로 변환 ( Float 타입 )
  • 그리디 : 반드시 특정 endTime 기준으로 + 1 초랑 겹치는 모든 구간을 찾는다.

알고리즘

  1. 주어진 리스트를 밀리초 단위로 시작시간와 끝나는시간 으로 변환한다.
  2. 파싱한 리스트를 2중으로 돌려 최대 겹치는 구간을 찾는다.

💻 코드

# https://school.programmers.co.kr/learn/courses/30/lessons/17676?language=python3

def solution(lines):
    def time_to_millisec(time: str):
        h, m, s = map(float, time.split(':'))
        return (float(h) * 3600 + float(m) * 60 + float(s))

    def to_start_end(line: str):
        date, time, duration = line.split()
        duration = float(duration[:-1])
        end_time = time_to_millisec(time)
        start_time = end_time - duration + 0.001
        return (start_time, end_time)

    intervals = list(map(to_start_end, lines))
    max_count = 0

    for i in range(len(intervals)):
        start_window = intervals[i][1]
        end_window = start_window + 1
        count = 0
        for start, end in intervals:
            if start < end_window and end >= start_window:
                count += 1
        max_count = max(max_count, count)

    return max_count
728x90