Skip to content

Commit a9940fd

Browse files
committed
[20250830] BOJ / G5 / 탑 / 김민진
1 parent 7afb102 commit a9940fd

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

zinnnn37/202508/30 BOJ G5 탑.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Stack;
4+
import java.util.StringTokenizer;
5+
6+
public class BJ_2493_탑 {
7+
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static final StringBuilder sb = new StringBuilder();
11+
private static StringTokenizer st;
12+
13+
private static int N;
14+
private static int[] height;
15+
16+
private static Stack<Integer> s;
17+
18+
public static void main(String[] args) throws IOException {
19+
init();
20+
sol();
21+
}
22+
23+
private static void init() throws IOException {
24+
N = Integer.parseInt(br.readLine());
25+
26+
height = new int[N];
27+
st = new StringTokenizer(br.readLine());
28+
for (int i = 0; i < N; i++) {
29+
height[i] = Integer.parseInt(st.nextToken());
30+
}
31+
32+
s = new Stack<>();
33+
s.push(0);
34+
sb.append("0 ");
35+
36+
br.close();
37+
}
38+
39+
private static void sol() throws IOException {
40+
for (int i = 1; i < N; i++) {
41+
// 현재 건물보다 높이가 높은 건물이 나올 때 까지 pop
42+
while (!s.isEmpty() && height[s.peek()] < height[i]) {
43+
s.pop();
44+
}
45+
46+
// 왼쪽에 현재 건물보다 높은 건물 부재
47+
if (s.isEmpty()) {
48+
sb.append("0 ");
49+
} else {
50+
sb.append(s.peek() + 1).append(" ");
51+
}
52+
s.push(i);
53+
}
54+
bw.write(sb.toString());
55+
bw.flush();
56+
bw.close();
57+
}
58+
59+
}
60+
```

0 commit comments

Comments
 (0)