Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8dbe4a7

Browse files
이지영: [PG] 뒤에 있는 큰 수 찾기_240913
1 parent a53f1e1 commit 8dbe4a7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /