Skip to content

Commit f5000ff

Browse files
committed
[Gold IV] Title: 부분합, Time: 268 ms, Memory: 24404 KB -BaekjoonHub
1 parent 8acfa0a commit f5000ff

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
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+
메모리: 24404 KB, 시간: 268 ms
8+
9+
### 분류
10+
11+
누적 합, 두 포인터
12+
13+
### 제출 일자
14+
15+
2025년 11월 14일 11:13:28
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) throws Exception {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
StringTokenizer st = new StringTokenizer(br.readLine());
8+
int N = Integer.parseInt(st.nextToken());
9+
int target = Integer.parseInt(st.nextToken());
10+
st = new StringTokenizer(br.readLine());
11+
int arr[] = new int[N];
12+
13+
for(int i=0; i<N; i++) {
14+
arr[i] = Integer.parseInt(st.nextToken());
15+
}
16+
int answer = Integer.MAX_VALUE;
17+
18+
int currentSum = 0;
19+
int start = 0;
20+
int end = 0;
21+
while(start<N) {
22+
if(end >=N && currentSum <target) break;
23+
24+
if(currentSum <= target) {
25+
if(end>=N) break;
26+
currentSum+=arr[end];
27+
end+=1;
28+
} else if(currentSum > target) {
29+
currentSum-= arr[start];
30+
start+=1;
31+
}
32+
if(currentSum>=target) {
33+
answer = Math.min(answer, end -start);
34+
}
35+
}
36+
if(answer == Integer.MAX_VALUE) {
37+
System.out.println(0);
38+
System.exit(0);
39+
}
40+
System.out.println(answer);
41+
}
42+
43+
/*
44+
* 5 1 3 5 10 7 4 9 2 8
45+
* 5 6 9 14 24
46+
* 끝나는 경우, end == start 이면서 end == N-1, end가 N-1인데 target보다 더 적을 때,
47+
*/
48+
}

0 commit comments

Comments
 (0)