[백준] 스카이라인 쉬운거 1863 java

오늘내일·2024년 7월 5일
0

이 문제는 아래의 블로그를 참고하였다.
https://dhelloper.tistory.com/65
블로그에서 해주는 설명을 꼼꼼히 읽어보면 이해가 쉬울 것 같다.

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

public class Main {
  // 스카이라인만을 보고 건물이 최소 몇 채 있는지 알아내자.
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n = Integer.parseInt(br.readLine());

    Stack<Integer> stack = new Stack<>();
    int count = 0;
    for (int i = 0; i < n; i++) {
      StringTokenizer st = new StringTokenizer(br.readLine());
      int x = Integer.parseInt(st.nextToken());
      int y = Integer.parseInt(st.nextToken());

      while (!stack.isEmpty() && y < stack.peek()) {
        count++;
        stack.pop();
      }

      if (!stack.isEmpty() && stack.peek() == y) {
        continue;
      }

      stack.push(y);
    }

    while (!stack.isEmpty() && 0 < stack.peek()) {
      count++;
      stack.pop();
    }

    System.out.println(count);

  }
}
profile
다시 시작합니다.

0개의 댓글