코딩테스트/백준

[BOJ 17298] 오큰수 (G4)

34suuuuu 2024. 12. 7. 23:28

📍 문제 

https://www.acmicpc.net/problem/17298

 

 

📍 전체 코드

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

public class boj_17298 {
	static int n;
	static int[] nums;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		StringBuilder sb = new StringBuilder();

		n = Integer.parseInt(st.nextToken());
		nums = new int[n];

		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < n; i++) {
			nums[i] = Integer.parseInt(st.nextToken());
		}

		Stack<Integer> stack = new Stack<Integer>();
		for (int i = 0; i < n; i++) {
			while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) {
				nums[stack.pop()] = nums[i];
			}
			stack.push(i);
		}

		while (!stack.isEmpty()) {
			nums[stack.pop()] = -1;
		}

		for (int i = 0; i < n; i++) {
			sb.append(nums[i]).append(' ');
		}
		System.out.println(sb);

	}
}

'코딩테스트 > 백준' 카테고리의 다른 글

[BOJ 7576] 토마토 (G5)  (2) 2024.12.09
[BOJ 2212] 센서 (G5)  (0) 2024.12.08
[BOJ 1697] 숨바꼭질 (S1)  (0) 2024.12.05
[BOJ 9663] N-Queen (G4)  (0) 2024.12.04
[BOJ 1759] 암호 만들기(G5)  (0) 2024.12.02