1806-g4 성공

This commit is contained in:
2026-03-16 13:58:01 +09:00
parent fc52790b41
commit 6ad8ca72e6

View File

@@ -0,0 +1,47 @@
# 부분합
import sys
input = sys.stdin.readline
def solution():
n, s = map(int, input().rstrip().split())
lst = list(map(int, input().rstrip().split()))
l, r = 0, 0
now = lst[0]
result = 100001
while l <= r:
if now >= s:
while now >= s:
now -= lst[l]
l += 1
result = min(result, r-(l-1)+1)
if r >= n-1:
break
if now < s:
r += 1
now += lst[r]
if result > 100000:
result = 0
print(result)
return
solution()
"""
걸린 시간: 18분
시간 복잡도: 두 포인터가 모든 원소를 가르키는 것이 최악이므로 O(n)이다.
해설: 투 포인터를 활용하여 오른쪽으로 값을 추가하고, 왼쪽으로 값을 제외한다.
현재 값이 s보다 크면, s보다 값이 작아질때까지 왼쪽 포인터를 오른쪽으로 옮기면서 값을 뺀다.
현재 값이 s보다 작으면 오른쪽 포인터를 오른쪽으로 옮기면서 값을 더한다.
마지막까지 result 값이 100000보다 크면 답이 없는 것이므로 result를 0으로 만든다.
"""