백준 - 최대 힙 ( 11279번, JAVA )

changi123·2024년 12월 4일
0
post-thumbnail

PriorityQueue ( https://www.acmicpc.net/problem/11279 )

풀이

  • 최소 힙과 반대로 큰 수부터 뽑는 우선순위큐 기본 문제
package problem_solving.queue;

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

public class BakeJoon_11279 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
		StringBuilder sb = new StringBuilder();
		while(n-- >  0 ) {
			int num = Integer.parseInt(sc.next());
			if( num !=0 ) {
				pq.offer(num);
			} else if ( num == 0 ) {
				if( pq.isEmpty()) {
					sb.append(0+" "+"\n");
				} else {
					sb.append(pq.poll()+ " "+ "\n");
				}
			}
			
		}
		
		System.out.println(sb.toString());
		

	}

}


profile
개발자 홍찬기 꾸준한 사람이 되자

0개의 댓글

관련 채용 정보