Coding Test

[Two Pointer] 2003: 수 들의 합2

kldaji 2021. 7. 7. 15:59

https://www.acmicpc.net/problem/2003

 

2003번: 수들의 합 2

첫째 줄에 N(1 ≤ N ≤ 10,000), M(1 ≤ M ≤ 300,000,000)이 주어진다. 다음 줄에는 A[1], A[2], …, A[N]이 공백으로 분리되어 주어진다. 각각의 A[x]는 30,000을 넘지 않는 자연수이다.

www.acmicpc.net

 

1. 코드

from sys import *

# 입력
N, M = map(int, stdin.readline().rstrip().split())
seq = list(map(int, stdin.readline().rstrip().split()))

# Two Pointer
start = 0
end = 0

total = 0
answer = 0

# Two Pointer Algorithm
while not (start == end == N):
    if total < M and end < N:
        total += seq[end]
        end += 1
    else:
        total -= seq[start]
        start += 1

    if total == M:
        answer += 1


print(answer)