Skip to content

Commit

Permalink
B_17299
Browse files Browse the repository at this point in the history
  • Loading branch information
likppi10 committed Oct 24, 2021
1 parent e84b4ce commit dca1666
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Algorithm_Baek/Basic/B_201/B_17299_오등큰수.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package B_201_자료구조1연습;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

public class B_17299_오등큰수 {

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
StringBuilder sb = new StringBuilder();
Stack<Integer> stack = new Stack<>();

int size = Integer.parseInt(br.readLine());
int[] a = new int[size];
int[] b = new int[1000001];
int[] c = new int[size];

st = new StringTokenizer(br.readLine(), " ");
for(int i=0; i<size; i++) {
a[i] = Integer.parseInt(st.nextToken());
b[a[i]]++;
}


for(int i=0; i<size; i++) {
while(!stack.isEmpty() && b[a[stack.peek()]]<b[a[i]]) c[stack.pop()] = a[i];
stack.add(i);
}
while(!stack.isEmpty()) c[stack.pop()] = -1;

for(int i=0; i<size; i++) sb.append(c[i] + " ");

System.out.println(sb);
}
}

0 comments on commit dca1666

Please sign in to comment.