File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+
7+ public static void main (String [] args ) throws IOException {
8+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
9+ StringTokenizer st;
10+
11+ int n = Integer . parseInt(br. readLine());
12+ int [] arr = new int [n];
13+ int [] result = new int [n];
14+ Stack<Integer > stk = new Stack<> ();
15+
16+ st = new StringTokenizer (br. readLine());
17+ for (int i = 0 ; i < n; i++ ) {
18+ arr[i] = Integer . parseInt(st. nextToken());
19+ }
20+
21+ for (int i = n - 1 ; i >= 0 ; i-- ) {
22+ while (! stk. isEmpty()) {
23+ if (stk. peek() > arr[i]) {
24+ result[i] = stk. peek();
25+ break ;
26+ } else stk. pop();
27+ }
28+ if (stk. isEmpty()) result[i] = - 1 ;
29+ stk. push(arr[i]);
30+ }
31+
32+ StringBuilder sb = new StringBuilder ();
33+ for (int i = 0 ; i < n; i++ ) {
34+ sb. append(result[i] + " " );
35+ }
36+
37+ System . out. println(sb. toString());
38+ }
39+ }
40+
41+ ```
You can’t perform that action at this time.
0 commit comments