Skip to content

Commit dbe40d5

Browse files
committed
[BOJ] #1806.부분합 / 골드4 / 45(X)
1 parent ca75229 commit dbe40d5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N, S = map(int, input().split())
5+
arr = list(map(int, input().split()))
6+
7+
def min_subarray_length(N, S, arr):
8+
# 초기화
9+
start, end = 0, 0
10+
current_sum = 0
11+
min_length = float('inf') # 최소 길이를 아주 큰 값으로 초기화
12+
13+
# 슬라이딩 윈도우 실행
14+
while end < N:
15+
# 현재 값을 current_sum에 추가
16+
current_sum += arr[end]
17+
18+
# current_sum이 S 이상이면 조건 만족
19+
while current_sum >= S:
20+
# 구간 길이 계산 후 최소 길이 갱신
21+
min_length = min(min_length, end - start + 1)
22+
# start를 오른쪽으로 이동하며 구간을 줄임
23+
current_sum -= arr[start]
24+
start += 1
25+
26+
# end를 오른쪽으로 이동
27+
end += 1
28+
29+
# 조건을 만족하는 구간이 없으면 0 반환, 아니면 최소 길이 반환
30+
return 0 if min_length == float('inf') else min_length
31+
32+
result = min_subarray_length(N, S, arr)
33+
print(result)

0 commit comments

Comments
 (0)