백준 1806 부분합

gmlwlswldbs·2021년 11월 1일
0

코딩테스트

목록 보기
67/130
n, s = map(int, input().split())
a = list(map(int, input().split()))

left = 0
right = 0
ans = n + 1 + s
tmp_sum = a[left]
loopout = False
while True:
    if tmp_sum >= s:
        ans = min(ans, right-left+1)
        if left == right:
            right += 1
            if right == n:
                loopout = True
                break
            tmp_sum += a[right]
        else:
            tmp_sum -= a[left]
            left += 1
    else:
        right += 1
        if right == n:
            loopout = True
            break
        tmp_sum += a[right]
    if loopout == True:
        break

if ans == n + 1 + s:
    print(0)
else:
    print(ans)

투포인터로 풀어봤다

0개의 댓글