Skip to content

Commit 10419ac

Browse files
committed
[BOJ] #1806. 부분합 / 골드4 / 60분 / 성공
1 parent b6443c1 commit 10419ac

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
3+
input = sys.stdin.readline
4+
5+
# 입력 받기
6+
N, S = map(int, input().split())
7+
numbers = list(map(int, input().split()))
8+
9+
# 초기 변수 설정
10+
interval_sum = 0
11+
end = 0
12+
count = N + 1 # 최소 길이 초기값 설정 (최대값)
13+
14+
# 슬라이딩 윈도우 알고리즘
15+
for start in range(N):
16+
# end를 확장하며 구간 합 계산
17+
while interval_sum < S and end < N:
18+
interval_sum += numbers[end]
19+
end += 1
20+
21+
# 부분합이 S 이상이면 최소 길이 갱신
22+
if interval_sum >= S:
23+
count = min(count, end - start)
24+
25+
# 구간 시작점을 이동하며 부분합 감소
26+
interval_sum -= numbers[start]
27+
28+
# 결과 출력 (조건 만족 구간이 없으면 0)
29+
print(0 if count == N + 1 else count)

0 commit comments

Comments
 (0)