728x90
📑문제링크
https://www.acmicpc.net/problem/1863
💻코드
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 answer = 0;
StringTokenizer st;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
while (!stack.isEmpty() && stack.peek() > y) {
// 현재 높이 보다 큰 경우
answer++;
stack.pop();
}
if (!stack.isEmpty() && stack.peek() == y) {
//현재 높이와 같은 경우
continue;
}
stack.push(y);
}
while (!stack.isEmpty() && stack.peek() > 0) {
answer++;
stack.pop();
}
System.out.println(answer);
}
}
728x90
'Algorithm > Baekjoon' 카테고리의 다른 글
[Algorithm/Baekjoon] 별 찍기 - 10 - 2447 (G5/JAVA) (1) | 2025.06.04 |
---|---|
[Algorithm/Baekjoon] 수 고르기 - 2230 (G5/JAVA) (0) | 2025.05.19 |
[Algorithm/Baekjoon] 1, 2, 3 더하기 7 - 15992 (S1/JAVA) (0) | 2025.05.12 |
[Algorithm/Baekjoon] 카우버거 알바생 - 17208 (G4/JAVA) (0) | 2025.03.10 |
[Algorithm/Baekjoon] 목장 건설하기 - 14925 (G4/JAVA) (0) | 2025.02.12 |