[Algorithm] [백준] 1449 - 수리공 항승 (그리디, 정렬)

myeonji·2022년 3월 12일
0

Algorithm

목록 보기
78/89

import sys
input = sys.stdin.readline

n, l = map(int, input().split())  # 물이 새는 곳 개수 n과 테이프 길이 l

position = list(map(int, input().split()))

position.sort()

start = 0
cnt = 0

for loc in position:
    if start < loc:
        # start에 테이프 붙이기
        # start + (l-1) 까지는 테이프 붙여짐
        start = loc + (l-1)
        cnt += 1

print(cnt)

start가 테이프를 붙이기 시작하는 위치이고, start + (l-1) 까지는 테이프가 붙여지게 된다.
따라서 for문을 돌 때, position에 포함되어 있는 숫자 중 start ~ start + (l-1) 사이의 숫자는 무시해도 된다. 이미 수리가 되었기 때문!

0개의 댓글