새발블로그

[백준/Java] 11279 최대 힙 본문

Problem Solving/Baekjoon

[백준/Java] 11279 최대 힙

EUG 2025. 7. 1. 13:58

문제

https://www.acmicpc.net/problem/11279

풀이방법

 

풀이

//java 함수 사용
import java.io.*;
import java.util.PriorityQueue;
import java.util.Collections;

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());
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
        int input;

        for (int i = 0; i < N; i++) {
            input = Integer.parseInt(br.readLine());
            if (input == 0) {
                if (maxHeap.isEmpty()) {
                    System.out.println(0);
                } else {
                    System.out.println(maxHeap.poll());
                }
            } else {
                maxHeap.offer(input);
            }
        }
    }
}

 

//함수 정의
import java.io.*;
import java.util.PriorityQueue;
import java.util.Comparator;

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());

        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                return b - a; 
            }
        });
        int input;
        for (int i = 0; i < N; i++) {
            input = Integer.parseInt(br.readLine());
            if (input == 0) {
                if (maxHeap.isEmpty()) {
                    System.out.println(0);
                } else {
                    System.out.println(maxHeap.poll());
                }
            } else {
                maxHeap.offer(input);
            }
        }
    }
}
//람다식
import java.io.*;
import java.util.PriorityQueue;
import java.util.Comparator;

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());

        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
        
        int input;
        for (int i = 0; i < N; i++) {
            input = Integer.parseInt(br.readLine());
            if (input == 0) {
                if (maxHeap.isEmpty()) {
                    System.out.println(0);
                } else {
                    System.out.println(maxHeap.poll());
                }
            } else {
                maxHeap.offer(input);
            }
        }
    }
}

//정렬 기준을 직접 정하고싶으면, comparator 객체를 생성자에 전달해야함
//Comparator<Integer> cmp = (a, b) -> b - a;
//PriorityQueue<Integer> maxHeap = new PriorityQueue<>(cmp);

'Problem Solving > Baekjoon' 카테고리의 다른 글

[백준/Java] 11652 카드  (0) 2025.07.01
[백준/Java] 1920 수 찾기  (1) 2025.07.01
[백준/Java] 18258 큐 2  (0) 2025.04.02
[백준/Java] 14425 문자열 집합  (0) 2025.03.30
[백준/Java] 4949 균형잡힌 세상  (0) 2025.03.30