다희의 코딩 성장일기
[백준] 11279. 최대 힙 (자바 JAVA) 본문
[ 문제 ] [백준] 11279. 최대 힙(자바 JAVA)
문제 링크 : https://www.acmicpc.net/problem/11279
11279번: 최대 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가
www.acmicpc.net
# 접근 방법 및 풀이
- 우선순위 큐를 이용하는 문제다.
- 우선순위 큐는 기본 최소힙 형태이므로 Collections.reverseOrder()로 생성해 poll할때 최대값이 나오게한다.
- 나머지는 문제 그대로 구현
- 코드참조
# 주의할 점
- 없음
JAVA 코드
package Silver;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class bj11279_최대힙 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
if(num == 0) {
if(!queue.isEmpty())
System.out.println(queue.poll());
else
System.out.println(0);
continue;
}
queue.add(num);
}
}
}
REVIEW
'Algorithm > 백준 BOJ' 카테고리의 다른 글
[백준] 11286. 절대값 힙 (자바 JAVA) (0) | 2021.09.07 |
---|---|
[백준] 1927. 최소 힙 (자바 JAVA) (0) | 2021.09.06 |
[백준] 16985. Maaaaaaaaaze (자바 JAVA) (0) | 2021.09.05 |
[백준] 5247. 불 (자바 JAVA) (0) | 2021.09.04 |
[백준] 5567. 결혼식 (자바 JAVA) (0) | 2021.09.03 |
Comments