-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2_heap_4.java
More file actions
36 lines (30 loc) · 925 Bytes
/
day2_heap_4.java
File metadata and controls
36 lines (30 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class day2_heap_4 {
/**
* 날짜 : 2021.01.04
* 문제 유형 : heap
* 문제 url : https://www.acmicpc.net/problem/1927
* 문제 요약
* - 제목 : Kth Largest Element in an Array
* : K번째로 큰 원소를 출력해라
*
*/
public static void main(String[] args) {
System.out.println(findKthLargest(new int[]{3, 2, 1, 5, 6, 4},2));
}
public static int findKthLargest(int[] nums, int k){
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for(int a : nums){
maxHeap.add(a);
}
for (int i = 0; i < k-1; i++) {
maxHeap.poll();
}
return maxHeap.poll();
}
}