Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22.02.23 - [BOJ] 11286. 절댓값 힙 #219

Closed
suhyunsim opened this issue Feb 23, 2022 · 0 comments
Closed

22.02.23 - [BOJ] 11286. 절댓값 힙 #219

suhyunsim opened this issue Feb 23, 2022 · 0 comments
Assignees
Labels
실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제 우선순위큐 Priority Queue

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Feb 23, 2022

문제

핵심 아이디어

  • 우선순위 큐 사용, 정렬

어려운 점, 실수

  • 우선순위큐를 처음 사용해서 어떻게 쓰는지 몰랐음. 정렬 기준만 정해주면 되는 문제였다.

풀이

package main.java.com.poogle.BOJ.Q11286;

import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((o1, o2) -> {
            if (Math.abs(o1) > Math.abs(o2)) return 1;
            else {
                if (Math.abs(o1) == Math.abs(o2)) return o1 - o2;
                else return -1;
            }
        });
        /* 같은 Comparable인데 다르게 표현할 수도 있음, 절댓값이 같으면 실제 숫자 기준으로, 아니라면 절댓값을 기준으로 오름차순 정렬
        PriorityQueue<Integer> queue = new PriorityQueue<>(((o1, o2) ->
        Math.abs(o1) == Math.abs(o2) ? Integer.compare(o1, o2) : Integer.compare(Math.abs(o1), Math.abs(o2))
        ));
        */
        for (int i = 0; i < n; i++) {
            int num = sc.nextInt();
            if (num == 0) {
                if (priorityQueue.isEmpty()) System.out.println("0");
                else System.out.println(priorityQueue.poll());
            } else {
                priorityQueue.add(num);
            }
        }
    }
}
@suhyunsim suhyunsim self-assigned this Feb 23, 2022
@suhyunsim suhyunsim added 실버 BOJ - 실버 우선순위큐 Priority Queue labels Feb 23, 2022
@suhyunsim suhyunsim added the 실패 시도했지만 맞지 못한 문제 label Mar 6, 2022
suhyunsim added a commit that referenced this issue Mar 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제 우선순위큐 Priority Queue
Projects
None yet
Development

No branches or pull requests

1 participant