File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed
Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments