Super Kawaii Cute Cat Kaoani [JAVA][Baekjoon] 11286번 절댓값 힙 ⭐️⭐️⭐️⭐️⭐️
728x90
SMALL

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

📌 접근 방식

  • PriorityQueue 사용 ! ➡️ 아직 우선순위 큐에 대해서는 낯설고 어렵다. 
  • 문제 조건
    •  x = 0 : 배열에서 절댓값이 가장 작은 값 출력 ➡️ 배열에서 해당 값 제거 (❗️ 절댓값이 같은 수가 여러 개가 있다면 음수 값을 우선 출력 및 제거)
    • x =! 0 : 배열에 정수를 넣음 (자동 정렬)
 

[JAVA] PriorityQueue 란? / 사용법

📌 PriorityQueue 란?일반적인 Queue는 FIFO(First In, First Out) 형식의 자료구조입니다. (선입 선출)우선순위 큐(Priority Queue)는 들어가는 순서와 상관없이 우선순위가 높은 데이터가 먼저 Out되는 자료구조

nyeroni.tistory.com

 

 

✅ PASS CODE

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 = Integer.parseInt(br.readLine());

        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((o1, o2) -> {
            int first_abs = Math.abs(o1);
            int second_abs = Math.abs(o2);

            if(first_abs == second_abs) {
                return o1 > o2 ? 1 : -1;
            }
            else{
                return first_abs - second_abs;
            }
        });

        for(int i = 0; i < N; i++){
            int a = Integer.parseInt(br.readLine());

            if(a == 0){
                if(priorityQueue.isEmpty()){
                    System.out.println("0");
                }
                else{
                    System.out.println(priorityQueue.poll());
                }
            }
            else{
                priorityQueue.add(a);
            }
        }
    }
}

 

 

728x90

 

728x90
LIST

BELATED ARTICLES

more