문제풀이/기타

[파이썬] [기타] 백준 1806 부분합

승무_ 2022. 2. 8. 14:12

문제

https://www.acmicpc.net/problem/1806

 

1806번: 부분합

첫째 줄에 N (10 ≤ N < 100,000)과 S (0 < S ≤ 100,000,000)가 주어진다. 둘째 줄에는 수열이 주어진다. 수열의 각 원소는 공백으로 구분되어져 있으며, 10,000이하의 자연수이다.

www.acmicpc.net

 

코드

n,s=map(int, input().split())
array=list(map(int, input().split()))

end=0
comp=array[0]
count=float('inf')

for i in range(n):
    while comp<s and end<n:
        end+=1
        if end == n:
            break
        comp+=array[end]

    if comp>=s:
        count=min(count, end-i+1)

    comp-=array[i]

if count == float('inf'):
    print(0)
else:
    print(count)