[알고리즘] 백준 - 17298 (오큰수) / 자바

배고픈메꾸리·2021년 2월 4일
0

알고리즘

목록 보기
25/128
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;

public class Test_17298 {
	public static void main(String[] args) throws Exception {
		Stack<Integer> stack = new Stack<>();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder("");

		int count = Integer.parseInt(br.readLine());
		int[] arr = new int[count];
		int[] answer = new int[count];
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		for (int i = 0; i < count; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}
		
		for (int i = count - 1; i >= 0; i--) {
			while (!stack.isEmpty()) {
				if (stack.peek() <= arr[i]) { // 2 <= 5
					stack.pop();
				} else {
					break;
				}
			}		
			if (stack.isEmpty()) {
				answer[i] = -1;
				stack.push(arr[i]);
				continue;
			}
			answer[i] = stack.peek();
			stack.push(arr[i]);
		}

		for (int i = 0; i < count; i++) {
			sb.append(answer[i]).append(" ");
		}
		System.out.print(sb);
	}
}

profile
FE 개발자가 되자

0개의 댓글