algorithm/문제 풀이

99클럽 코테 스터디 2일차 TIL - 백준 11279 (최대 힙) java

ssoheeh 2024. 4. 2. 22:50

 

 

java로 최대/최소 힙 구현 -> PriorityQueue 사용

import java.util.*;
import java.io.*;


public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N;
        N = Integer.parseInt(br.readLine());
        int num;
        PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)->(b-a));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < N; i++) {
            num = Integer.parseInt(br.readLine());
            if(num==0) {
                if (pq.isEmpty()) sb.append(0).append("\n");
                else {
                    sb.append(pq.peek()).append("\n");
                    pq.poll();
                }
            }else{
                pq.add(num);
            }

        }
        System.out.print(sb);




        

        br.close();

    }
}

 

 

최대힙 

PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)->(b-a));

 

최소힙 

PriorityQueue<Integer> pq = new PriorityQueue<>();

 

 

오늘의 회고

  • 자료구조를 특정지어 문제가 나오지 않아도 풀 수 있게 완벽히 이해해서 문제풀이를 하자
  • 내일(4/3) 학습 예정 : 백트래킹 알고리즘 문제 풀고 정리, 스프링부트 프로젝트 강의 듣기