File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class Solution {
4
+
5
+ static class State implements Comparable <State >{
6
+ int num , idx ;
7
+ public State (int num , int idx ) {
8
+ this .num = num ;
9
+ this .idx = idx ;
10
+ }
11
+ @ Override
12
+ public int compareTo (State other ){
13
+ if (this .num == other .num ){
14
+ return this .idx -other .idx ;
15
+ }
16
+ return this .num - other .num ;
17
+ }
18
+ @ Override
19
+ public String toString (){
20
+ return "n:" +this .num +" i:" +this .idx ;
21
+ }
22
+ }
23
+
24
+ public int [] solution (int [] numbers ) {
25
+ int N = numbers .length ;
26
+ int [] answer = new int [N ];
27
+ for (int i =0 ; i <N ; i ++){
28
+ answer [i ] = -1 ;
29
+ }
30
+
31
+ PriorityQueue <State > pq = new PriorityQueue <>();
32
+ for (int i =0 ; i <N ; i ++){
33
+ int val = numbers [i ];
34
+ while (!pq .isEmpty () && pq .peek ().num < val ){
35
+ State pre = pq .poll ();
36
+ answer [pre .idx ] = val ;
37
+ }
38
+ pq .add (new State (val , i ));
39
+ }
40
+
41
+ return answer ;
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments