본문 바로가기

Algorithm/백준

백준 17298 오큰수

package practice;

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

// https://st-lab.tistory.com/196 페이지 참조
public class Boj17298 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        Stack<Integer> stack = new Stack<>();
        int[] arr = new int[n];
        int[] result = new int[n];
        for(int i= 0; i< n ; i ++){
            arr[i] = Integer.parseInt(st.nextToken());
            result[i] = -1;
        }
        br.close();
        for(int i = 0 ; i < n; i ++){
            while(!stack.isEmpty() && arr[stack.peek()] < arr[i]){
                result[stack.pop()] = arr[i];
            }

            stack.push(i);
        }

        for (int i : result) {
            sb.append(i + " ");
        }

        System.out.println(sb);

    }

}

'Algorithm > 백준' 카테고리의 다른 글

백준 28278 스택 2  (0) 2024.07.25
백준 24511 queuestack  (0) 2024.07.25
백준 12789 도키도키 간식드리미  (0) 2024.07.25
백준 11866 요세푸스문제 0  (0) 2024.07.25
백준2164 카드2  (0) 2024.07.25