Baekjoon - 11279

Tadap·2023년 10월 21일
0

Baekjoon

목록 보기
59/94
post-custom-banner

문제

Solved.ac Class3++

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());

		int size = Integer.parseInt(br.readLine());
		for (int i = 0; i < size; i++) {
			int data = Integer.parseInt(br.readLine());
			if (data == 0) {
				if (queue.isEmpty()) {
					sb.append("0").append("\n");
				} else {
					sb.append(queue.remove()).append("\n");
				}
			} else {
				queue.add(data);
			}
		}
		System.out.println(sb);
	}
}

성공

ToKotlin

fun main() {
    val sb = StringBuilder()
    val priorityQueue = PriorityQueue<Int>(Comparator.reverseOrder())

    val size = readln().toInt()
    for (i in 0..<size) {
        val data = readln().toInt()
        if (data == 0) {
            if (priorityQueue.isEmpty()) {
                sb.append("0").append("\n")
            } else {
                sb.append(priorityQueue.remove()).append("\n")
            }
        } else {
            priorityQueue.add(data)
        }
    }
    print(sb)
}

post-custom-banner

0개의 댓글