Skip to content

Commit 0bf7f31

Browse files
committed
[Gold V] Title: 두 개의 탑, Time: 180 ms, Memory: 19568 KB -BaekjoonHub
1 parent 7276afb commit 0bf7f31

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

백준/Gold/2118. 두 개의 탑/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 20144 KB, 시간: 1236 ms
7+
메모리: 19568 KB, 시간: 180 ms
88

99
### 분류
1010

1111
누적 합, 두 포인터
1212

1313
### 제출 일자
1414

15-
2025년 6월 5일 18:52:23
15+
2025년 6월 5일 19:22:25
1616

1717
### 문제 설명
1818

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
import java.io.*;
2-
import java.util.*;
32

43
public class Main {
54

6-
static int n;
7-
static int[] arr, cw, ccw;
5+
static int n, sum;
6+
static int[] arr;
87
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
98
public static void main(String[] args) throws Exception{
109
inputSetting();
1110
System.out.println(findMaxLen());
1211
}
1312

1413
static int findMaxLen(){
15-
int answer = 0;
14+
int answer = -1;
15+
int r, l, min, now;
16+
r = 0;
17+
l = 0;
18+
now = arr[0];
1619

17-
for(int i = 0; i < n; i++){
18-
for(int j = i + 1; j < n; j++){
19-
answer = Math.max(answer, Math.min(cw[j] - cw[i], cw[n] - (cw[j] - cw[i])));
20-
}
20+
while(l <= r && r < n){
21+
min = Math.min(now, sum - now);
22+
answer = Math.max(min, answer);
23+
24+
if(min == now) now += arr[++r];
25+
else now -= arr[l++];
2126
}
2227
return answer;
2328
}
2429

2530
static void inputSetting() throws Exception{
2631
n = Integer.parseInt(br.readLine());
27-
arr = new int[n];
28-
cw = new int[n + 1];
32+
arr = new int[n + 1];
2933

3034
for(int i = 0; i < n; i++){
3135
arr[i] = Integer.parseInt(br.readLine());
32-
cw[i + 1] += cw[i] + arr[i];
36+
sum += arr[i];
3337
}
3438
}
3539
}

0 commit comments

Comments
 (0)