[Gold IV] Title: 부분합, Time: 380 ms, Memory: 42244 KB -BaekjoonHub
This commit is contained in:
28
백준/Gold/1806. 부분합/README.md
Normal file
28
백준/Gold/1806. 부분합/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# [Gold IV] 부분합 - 1806
|
||||
|
||||
[문제 링크](https://www.acmicpc.net/problem/1806)
|
||||
|
||||
### 성능 요약
|
||||
|
||||
메모리: 42244 KB, 시간: 380 ms
|
||||
|
||||
### 분류
|
||||
|
||||
누적 합, 두 포인터
|
||||
|
||||
### 제출 일자
|
||||
|
||||
2025년 9월 14일 22:49:17
|
||||
|
||||
### 문제 설명
|
||||
|
||||
<p>10,000 이하의 자연수로 이루어진 길이 N짜리 수열이 주어진다. 이 수열에서 연속된 수들의 부분합 중에 그 합이 S 이상이 되는 것 중, 가장 짧은 것의 길이를 구하는 프로그램을 작성하시오.</p>
|
||||
|
||||
### 입력
|
||||
|
||||
<p>첫째 줄에 N (10 ≤ N < 100,000)과 S (0 < S ≤ 100,000,000)가 주어진다. 둘째 줄에는 수열이 주어진다. 수열의 각 원소는 공백으로 구분되어져 있으며, 10,000이하의 자연수이다.</p>
|
||||
|
||||
### 출력
|
||||
|
||||
<p>첫째 줄에 구하고자 하는 최소의 길이를 출력한다. 만일 그러한 합을 만드는 것이 불가능하다면 0을 출력하면 된다.</p>
|
||||
|
||||
27
백준/Gold/1806. 부분합/부분합.py
Normal file
27
백준/Gold/1806. 부분합/부분합.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import sys
|
||||
from collections import deque
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
n, s = map(int, input().split())
|
||||
arr = deque(map(int, input().split()))
|
||||
|
||||
start, end = 0, 0
|
||||
tot = 0
|
||||
min_length = sys.maxsize
|
||||
|
||||
while True:
|
||||
if tot >= s:
|
||||
min_length=(min(min_length, end - start))
|
||||
tot -= arr[start]
|
||||
start += 1
|
||||
elif end == n:
|
||||
break
|
||||
else:
|
||||
tot += arr[end]
|
||||
end += 1
|
||||
|
||||
if(min_length == sys.maxsize):
|
||||
print(0)
|
||||
else:
|
||||
print(min_length)
|
||||
Reference in New Issue
Block a user