[BOJ][๐ŸŸก4][๋ฐฑ์ค€#01806] ๋ถ€๋ถ„ํ•ฉ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1806


๋ฌธ์ œ

10,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ๊ธธ์ด N์งœ๋ฆฌย ์ˆ˜์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ์ˆ˜์—ด์—์„œ ์—ฐ์†๋œ ์ˆ˜๋“ค์˜ ๋ถ€๋ถ„ํ•ฉ ์ค‘์— ๊ทธ ํ•ฉ์ด S ์ด์ƒ์ด ๋˜๋Š” ๊ฒƒ ์ค‘, ๊ฐ€์žฅ ์งง์€ ๊ฒƒ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

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


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ์ตœ์†Œ์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์ผ ๊ทธ๋Ÿฌํ•œ ํ•ฉ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด 0์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

10 15
5 1 3 5 10 7 4 9 2 8


์ถœ๋ ฅ

2


My Sol

import sys
input = sys.stdin.readline

N, S = map(int, input().split())
arr = list(map(int, input().split()))
sum_lst = [0]*(N+1)

for i in range(1, N+1):
    sum_lst[i] = sum_lst[i-1] + arr[i-1]

ans = 100001
s = 0
e = 1

while s!=N:
    if sum_lst[e] - sum_lst[s] >= S:
        if e-s < ans:
            ans = e-s
        s += 1

    else:
        if e != N:
            e += 1
        else:
            s += 1

print(ans) if ans < 100001 else print(0)

ํˆฌํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์–ด๋–ป๊ฒŒ ํ‘ธ๋Š”์ง€ ๋ชฐ๋ผ์„œ ํ’€์ด(์ถœ์ฒ˜)๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

์ˆซ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉด ๋ˆ„์ ํ•ฉ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , 0๊ณผ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด S ๊ฒฝ๊ณ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํฌ์ธํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ์ด๋™์‹œํ‚ค๋ฉด์„œ ๋ˆ„์ ํ•ฉ ์ฐจ์ด๊ฐ€ S ์ด์ƒ์ด๋ผ๋ฉด e์™€ s ๊ฐ’ ์ฐจ์ด๋ฅผ ์ตœ์†Ÿ๊ฐ’ ans์— ๊ณ„์† ๊ฐฑ์‹ ํ•˜๋ฉฐ ์ธ๋ฑ์Šค๊ฐ€ N ์ผ ๊นจ๊นŒ์ง€ ๋‹ฌ๋ ค๊ฐ„๋‹ค. S๋ณด๋‹ค ๋ˆ„์ ํ•ฉ ์ฐจ์ด๊ฐ€ ์ž‘์œผ๋ฉด e๋ฅผ ๋Š˜๋ ค์ฃผ์–ด S์— ๊ทผ์ ‘ํ•˜๋„๋ก ํ•œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

answer = 100000

n, m = map(int,input().split())
nums = list(map(int,input().split()))

i = 0
j = 0
s = nums[0]

l = 0
while i < n:
    
    if s >= m :
        l = j - i + 1
        if answer > l:
            answer = l
        
        s -= nums[i]
        i += 1
        
    else:
        j += 1
        if j == n:
            break
        s += nums[j]
    
print(0 if answer == 100000 else answer)

์—ฐ์‚ฐ์‹œ๊ฐ„์ด 20% ์ •๋„ ๋น ๋ฅธ ํ’€์ด๋ฅผ ๋ฐœ๊ฒฌํ•ด ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. ๋‚˜๋Š” ๋ˆ„์ ํ•ฉ์„ ๊ณ„์‚ฐํ•ด์ฃผ๊ณ  ์‹œ์ž‘ํ–ˆ๋Š”๋ฐ, ์ด ๋ถ„์€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด ์ˆซ์ž ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ’์„ ๋”ํ•ด์ฃผ๊ณ  ๋นผ์ฃผ๋Š” ๊ณผ์ •์„ ํ†ตํ•ด ์ฒ˜์Œ์— ๋ˆ„์ ํ•ฉ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ