Skip to content

Commit c943416

Browse files
committed
[Gold IV] Title: 부분합, Time: 112 ms, Memory: 120904 KB -BaekjoonHub
1 parent 76916b0 commit c943416

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Gold IV] 부분합 - 1806
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1806)
4+
5+
### 성능 요약
6+
7+
메모리: 120904 KB, 시간: 112 ms
8+
9+
### 분류
10+
11+
누적 합, 두 포인터
12+
13+
### 제출 일자
14+
15+
2026년 4월 4일 03:28:51
16+
17+
### 문제 설명
18+
19+
<p>10,000 이하의 자연수로 이루어진 길이 N짜리 수열이 주어진다. 이 수열에서 연속된 수들의 부분합 중에 그 합이 S 이상이 되는 것 중, 가장 짧은 것의 길이를 구하는 프로그램을 작성하시오.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 N (10 ≤ N < 100,000)과 S (0 < S ≤ 100,000,000)가 주어진다. 둘째 줄에는 수열이 주어진다. 수열의 각 원소는 공백으로 구분되어져 있으며, 10,000이하의 자연수이다.</p>
24+
25+
### 출력
26+
27+
<p>첫째 줄에 구하고자 하는 최소의 길이를 출력한다. 만일 그러한 합을 만드는 것이 불가능하다면 0을 출력하면 된다.</p>
28+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N, S = map(int, input().split())
5+
nums = list(map(int, input().split()))
6+
7+
start, end = 0, 0
8+
cur = 0
9+
ans = N+1
10+
11+
while True:
12+
if cur >= S:
13+
ans = min(ans, end - start)
14+
cur -= nums[start]
15+
start += 1
16+
elif end == N:
17+
break
18+
else:
19+
cur += nums[end]
20+
end += 1
21+
print(ans if ans != N+1 else 0)

0 commit comments

Comments
 (0)