DevYoon

[๋ฐฑ์ค€] 1806. ๋ถ€๋ถ„ํ•ฉ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 1806. ๋ถ€๋ถ„ํ•ฉ (Python)

gimewn 2022. 8. 24. 00:09

link ๐Ÿ”— https://www.acmicpc.net/problem/1806

 

1806๋ฒˆ: ๋ถ€๋ถ„ํ•ฉ

์ฒซ์งธ ์ค„์— N (10 ≤ N < 100,000)๊ณผ S (0 < S ≤ 100,000,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ˆ˜์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ˆ˜์—ด์˜ ๊ฐ ์›์†Œ๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์ ธ ์žˆ์œผ๋ฉฐ, 10,000์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

 

๐Ÿ“Œ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ€๋ถ„ํ•ฉ์„ ๊ตฌํ•ด์ฃผ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๐Ÿ“Œ 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)