카테고리 없음

[Java] 백준 - 2493번: 탑

C0MPAS 2024. 4. 30. 16:41

4월 30일(화) - 자료 구조(2493번)

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


최초 생각 정리

 


문제점


풀이

(풀이출처 -> https://moonsbeen.tistory.com/204)

import java.io.*;
import java.util.*;
 
public class Main {
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        Stack<int[]> stack = new Stack<>();
        for(int i = 1; i <= n; i++) {
            int top = Integer.parseInt(st.nextToken());
            while(!stack.isEmpty()) {
                if(stack.peek()[1] >= top) {
                    System.out.print(stack.peek()[0] + " ");
                    break;
                }
                stack.pop();
            }
            if(stack.isEmpty()) {
                System.out.print("0 ");
            }
            stack.push(new int[] {i, top}); 
        }
    }
}