반응형
1. 문제 소개
- 그리디 문제에 해당한다.
https://www.acmicpc.net/problem/1449
2. 코드
def set_test_case():
N,L = input().split(" ")
N,L = int(N),int(L)
leak_pos = []
leak_pos_org = input().split(" ")
for v in leak_pos_org:
leak_pos += [int(v)]
return N,L,sorted(leak_pos)
def solution(N,L,leak_pos):
cnt = 1
l = L
start = 0
end = start+1
while(end < N):
if (leak_pos[end]-leak_pos[start]) >= L :
cnt+=1
start = end
end = start +1
else:
end+=1
print(cnt)
def main():
N,L,leak_pos = set_test_case()
solution(N,L,leak_pos)
if __name__=='__main__':
main()
3.코멘트
- 자연수 범위에는 0이 들어가지 않는데, 0의 입력케이스 까지 생각했다가 코드를 다시 수정했다..
반응형
'Algorithm' 카테고리의 다른 글
leetcode 알고리즘 - Search Insert Position (0) | 2021.11.29 |
---|---|
leetcode 알고리즘 - MaximumSubarray (0) | 2021.11.25 |
leetcode 알고리즘 - TwoSum (0) | 2021.10.24 |
Baekjoon 백준 알고리즘 - 스위치 켜고 끄기 ( 1244 ) (0) | 2021.10.17 |
Baekjoon 백준 알고리즘 - 되돌리기 ( 1360 ) (0) | 2021.07.26 |