λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸƒ algorithm/programmers

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ Level 3 - 좔석 νŠΈλž˜ν”½

by HandHand 2021. 3. 1.

문제

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ - 좔석 νŠΈλž˜ν”½

풀이 κ³Όμ •

주어진 λ¬Έμžμ—΄μ„ 적절히 λ³€ν™˜ν•œ λ’€ 일정 크기의 μœˆλ„μš°μ™€ μ΅œλŒ€ν•œ 많이 κ²ΉμΉ˜λŠ” νŠΈλž˜ν”½μ˜ 수λ₯Ό κ΅¬ν•˜λ©΄ λœλ‹€.

λ¬Έμžμ—΄ μ²˜λ¦¬ν•˜κΈ°

λ°€λ¦¬μ„Έμ»¨λ“œ λ‹¨μœ„μ˜ μ‹œκ°„ 계산은 μ²˜μŒμ΄λΌμ„œ μ–΄λ–»κ²Œ ν•΄μ€˜μ•Όν• μ§€ κ³ λ―Όν•˜λ‹€κ°€ float ν˜•μœΌλ‘œ λ³€ν™˜ν•˜μ—¬

μ‚¬μš©ν•˜λ©΄ 효율적일 것 κ°™μ•„μ„œ 이 방법을 μ‚¬μš©ν–ˆλ‹€.

λ‹€λ§Œ μ „λ‚ λΆ€ν„° νŠΈλž˜ν”½μ΄ μ‹œμž‘ λ˜μ—ˆμ„ 경우 νŠΈλž˜ν”½ μ’…λ£Œ μ‹œκ°„ - μ†Œμš”λœ μ‹œκ°„μ΄ μŒμˆ˜κ°€ λ˜λŠ” κ²½μš°κ°€ μžˆμ–΄μ„œ

이 뢀뢄에 λŒ€ν•œ μ˜ˆμ™Έμ²˜λ¦¬λŠ” λ”°λ‘œ ν•΄μ€˜μ•Ό ν–ˆλ‹€.

λ˜ν•œ floatν˜•μœΌλ‘œ λ³€ν™˜ν•˜κ³  μ‚°μˆ μ—°μ‚°μ„ μˆ˜ν–‰ν•˜λ©΄ μ†Œμˆ˜μ  μžλ¦¬μ— μ˜ˆμƒν•˜μ§€ λͺ»ν•œ 값듀이 좜λ ₯λ˜λŠ” κ²½μš°κ°€ μžˆμ–΄μ„œ

round ν•¨μˆ˜λ₯Ό 톡해 μ†Œμˆ˜μ  자리수λ₯Ό κ³ μ •μ‹œμΌœμ£ΌλŠ” μž‘μ—…μ΄ ν•„μš”ν–ˆλ‹€.

μ΅œλŒ€ μ²˜λ¦¬λŸ‰ κ³„μ‚°ν•˜κΈ°

μ΅œλŒ€ μ²˜λ¦¬λŸ‰μ„ κ΅¬ν•˜λŠ” 뢀뢄이 μ˜ˆμƒλ³΄λ‹€ μ’€ κΉŒλ‹€λ‘œμ› λ‹€.

μ²˜μŒμ—λŠ” 첫 νŠΈλž˜ν”½μ„ κΈ°μ€€μœΌλ‘œ λ°€λ¦¬μ„Έμ»¨λ“œ λ‹¨μœ„λ‘œ μœˆλ„μš°λ₯Ό μ¦κ°€μ‹œν‚€λ©΄μ„œ 전체 탐색을 μˆ˜ν–‰ν–ˆμ§€λ§Œ μ‹œκ°„μ΄ˆκ³Όκ°€ λ°œμƒν–ˆλ‹€.

μ΅œμ ν™”λ₯Ό μœ„ν•΄ Greedyν•œ λ°©λ²•μœΌλ‘œ μ ‘κ·Όν•΄μ„œ λλ‚˜λŠ” μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ νŠΈλž˜ν”½ 갯수λ₯Ό μ²΄ν¬ν•˜λ €κ³  ν–ˆμ§€λ§Œ

이 κ²½μš°μ— 검사λ₯Ό λͺ»ν•˜κ³  μ§€λ‚˜μΉ˜λŠ” 뢀뢄이 μžˆμ–΄μ„œ λͺ‡ 가지 ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€μ— λŒ€ν•΄ 잘λͺ»λœ κ²°κ³Όλ₯Ό 좜λ ₯함에 따라 λ‹€λ₯Έ 방법이 ν•„μš”ν–ˆλ‹€.

도무지 μ΅œμ ν™” 방법이 λ– μ˜€λ₯΄μ§€ μ•Šμ•„ 카카였 문제 해섀을 μ°Έκ³ ν•˜μ—¬ νŠΈλž˜ν”½μ΄ μƒˆλ‘œ λ“€μ–΄μ˜€κ±°λ‚˜ λλ‚˜λŠ” λΆ€λΆ„μ—μ„œλ§Œ

μœˆλ„μš°μ™€ κ²ΉμΉ˜λŠ” νŠΈλž˜ν”½ 갯수λ₯Ό μ„Έμ–΄μ£Όλ©΄ λœλ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€.

문제λ₯Ό μ°¨κ·Όμ°¨κ·Ό λ”°μ Έλ³΄λŠ” μ—°μŠ΅μ΄ 아직 많이 ν•„μš”ν•œ 것 κ°™λ‹€.

μ½”λ“œ



# μ‹œμž‘μ‹œκ°„κ³Ό λλ‚˜λŠ” μ‹œκ°„μ„ λ°˜ν™˜
def parse_time(traffic_change, time_str):
    end, duration = time_str.split()[1:]
    duration = float(duration.split('s')[0])

    end_h, end_m, end_s = list(map(float, end.split(':')))

    end = 3600 * end_h + 60 * end_m + end_s
    start = end - duration + 0.001
    if start < 0:
        start = 0

    traffic_change.add(round(start, 3))
    traffic_change.add(round(end, 3))
    return round(start, 3), round(end, 3)


def solution(lines):
    answer = 0

    traffic_change = set()
    timeline = []
    for line in lines:
        timeline.append(parse_time(traffic_change, line))

    # λλ‚˜λŠ” μ‹œκ°„μœΌλ‘œ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ - μ‹œμž‘ν•˜λŠ” μ‹œκ°„μœΌλ‘œ 정렬해도 상관 없을 λ“― ν•˜λ‹€.
    timeline.sort(key=lambda x: x[1])

    for point in traffic_change:
        window = point
        window_end = round(window + 1.0 - 0.001, 3)

        hit = []
        for traffic in timeline:
            traffic_start = traffic[0]
            traffic_end = traffic[1]

            # ν˜„μž¬ window 에 ν•΄λ‹Ήν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄
            if traffic_start > window_end or traffic_end < window:
                continue
            else:
                hit.append(traffic)
        answer = max(answer, len(hit))

    return answer


lines = [
    "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-15 21:00:00.748 2.31s",
    "2016-09-15 21:00:00.966 0.381s",
    "2016-09-15 21:00:02.066 2.62s"
]

print(solution(lines))  # κ²°κ³ΌλŠ” 7

λ°˜μ‘ν˜•

πŸ’¬ λŒ“κΈ€