-문제 :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)

'백준 > Greedy' 카테고리의 다른 글

[그리디/백준] 1543번: 문서 검색  (0) 2022.07.05
[그리디/백준] 2437번: 저울  (0) 2022.07.05
[그리디/백준] 1080번: 행렬  (0) 2022.07.04
[그리디/백준] 1049번: 기타줄  (0) 2022.07.04
[그리디/백준] 16953번: A -> B  (0) 2022.07.01

+ Recent posts