백준/Greedy

[그리디/백준] 1449번: 수리공 항승

ydin 2022. 7. 5. 10:11

-문제 :https://www.acmicpc.net/problem/1449

 

-정답풀이 :

l단위 만큼 이동하면서 그 범위안에 leak이 포함된다면 통과하고, 아니면 테이프를 붙이고 갯수를 늘려가는 방식으로 진행한다

n,l = map(int,input().split())
leak = list(map(int,input().split()))
leak.sort()
count=1
start=leak[0]
end=leak[0]+l
for i in range(n):
    if start <= leak[i] < end:
        continue
    else:
        start=leak[i]
        end=leak[i]+l
        count+=1
print(count)

 

-IndexError가 난 풀이

pythontutor로 예시 돌렸을 땐 다 정답이 나오는데 왜 계속 IndexError가 나는지 모르겠다

n,l = map(int,input().split())
leak = list(map(int,input().split()))
leak.sort()
result=[0]*(1001)
count=0

for i in range(n):
    if result[leak[i]]==0:
        for j in range(leak[i],leak[i]+l+1):
            result[j]=1
        count+=1
    else:
        continue
print(count)