2024년 5월 10일 알고리즘 문제풀이

문제

난이도

Lv.1

코드

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from heapq import heappop, heappush

def solution(n, m, section):
    answer = 0
    arr = []
    for x in section:
        heappush(arr,x)
    idx = heappop(arr)
    end = idx + m -1
    cnt = 1
    while arr:
        if arr[0] <= end:
            heappop(arr)
            continue
        else:
            idx = heappop(arr)
            end = idx + m -1
            cnt += 1
    return cnt

최소힙을 사용하는 아이디어가 금방 떠올라서 쉽게 풀었다.