본문 바로가기

파이썬 알고리즘 (인프런)/탐색&시물레이션

5. 수들의 합

# 수들의 합

def run(N, M, a):
    cnt = 0
    ps = pe = tot = 0  # tot = sum(a[ps:pe])
    while True:
        if tot < M:
            if pe == N:
                break
            tot += a[pe]
            pe += 1
        else:
            if tot == M:
                cnt += 1
            tot -= a[ps]
            ps += 1

    return cnt


N, M = map(int, input().split())
a = list(map(int, input().split()))
print(run(N, M, a))

'파이썬 알고리즘 (인프런) > 탐색&amp;amp;시물레이션' 카테고리의 다른 글

7. 사과나무  (0) 2021.02.09
6. 격자판 최대합  (0) 2021.02.09
4. 두 리스트 합치기  (0) 2021.02.09
3. 카드 역배치  (0) 2021.02.09
2. 숫자만 추출  (0) 2021.02.09