백준 1806 - 부분합 (파이썬)

Updated:

Answer

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

left = 0
right = 0
total = 0
answer = float('inf')

while True:
    if total >= s:
        answer = min(answer, right - left)
        total -= a[left]
        left += 1
    elif right == n:
        break
    else:
        total += a[right]
        right += 1
        
if answer == float('inf'):
   print(0)
else:
   print(answer)

Categories:

Updated: