[백준] 2493번 탑(java)

728x90
반응형

 

 

package day03;


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

//5
//
//5 3 2 1 4

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

}
728x90
반응형
TAGS.

Comments