QHEAP1 Hackerrank Solution Java | Data Structure
For Explanation:
Sample Input
STDIN Function ----- -------- 5 Q = 5 1 4 insert 4 1 9 insert 9 3 print minimum 2 4 delete 4 3 print minimum
Sample Output
4
9
Code:
import java.io.*;import java.util.*;
public class Solution {
public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner scn = new Scanner(System.in); PriorityQueue<Integer> heap = new PriorityQueue<Integer>(); int q = scn.nextInt(); while(q-->0){ int q1 = scn.nextInt(); if(q1==1){ int num = scn.nextInt(); heap.add(num); }else if(q1==2){ int num = scn.nextInt(); heap.remove(num); }else if(q1==3){ System.out.println(heap.peek()); } } }}
Comments
Post a Comment