티스토리 뷰

Algorithms/BOJ

백준 17298

PeonyF 2023. 1. 11. 02:14
반응형
import java.util.Scanner;
import java.util.Stack;

public class Q17298 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Stack<Integer> stack = new Stack<Integer>();
        int n = sc.nextInt();
        int [] arr = new int[n];

        for(int i=0; i<n; i++){
            arr[i]=sc.nextInt();
        }

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

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

        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; i++) {
            sb.append(arr[i]).append(' ');
        }

        System.out.println(sb);

    }
}

출저: https://st-lab.tistory.com/196

반응형

'Algorithms > BOJ' 카테고리의 다른 글

Q10972 다음순열  (0) 2020.03.06
백준 Q.1157 단어 공부  (0) 2020.01.08
백준 Q.1159 농구 경기  (0) 2020.01.08
백준 Q.1302 베스트셀러  (0) 2020.01.07
백준 Q.9375 패션왕 신해빈  (0) 2020.01.06
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함