DevYoon
[๋ฐฑ์ค] 1806. ๋ถ๋ถํฉ (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/1806
๐ ํฌํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ๋ถํฉ์ ๊ตฌํด์ฃผ๋ ๋ฌธ์ ์๋ค.
๐ sum์ด S๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด ์ด๋๊น์ง ๊ธธ์ด๋ฅผ ์ค์ผ ์ ์๋์ง ํ์ธํ๊ณ
๐ sum์ด S๋ณด๋ค ์์ผ๋ฉด e๋ฅผ ๋๋ ค ๋ํด์ค๋ค.
import sys
N, S = map(int, sys.stdin.readline().split())
seq = list(map(int, sys.stdin.readline().split()))
s, e = 0, 0
res = 9e8
sum = seq[0]
while True:
if sum >= S:
sum -= seq[s]
res = min(res, e - s+1)
s += 1
else:
e += 1
if e == N:
break
sum += seq[e]
if res == 9e8:
print(0)
else:
print(res)