백준 2003 수들의 합 2

gmlwlswldbs·2021년 11월 1일
0

코딩테스트

목록 보기
66/130
n, m = map(int, input().split())
a = [-1] + list(map(int, input().split()))

cnt = 0
right = 1
left = 1
tmp_sum = a[left]
loopout = False
while True:
    if tmp_sum > m:
        tmp_sum -= a[left]
        left += 1
    elif tmp_sum == m:
        cnt += 1
        if left == right:
            right += 1
            if right == n+1 or left > right:
                loopout = True
                break
            tmp_sum += a[right]
        else:
            tmp_sum -= a[left]
            left += 1
    elif tmp_sum < m:
        right += 1
        if right == n+1 or left > right:
            loopout = True
            break
        tmp_sum += a[right]
    if loopout == True:
        break
print(cnt)

투포인터로 풀어봤다

0개의 댓글