Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Wednesday, May 24, 2017

QHEAP1 Hackerrank Solution


Problem :
1: Add element k when 1 pressed
2: delete element K
3: Print minimum element


Solution:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        PriorityQueue<Integer> heap=new PriorityQueue<Integer>(); // implementing heap variable
        Scanner ab=new Scanner(System.in);
        int n=ab.nextInt();
        while(n-->0)
            {
            int k=ab.nextInt();
             int data;
            switch(k)
                {
                case 1:
                 data=ab.nextInt();
                heap.offer(data); // add data
                break;
                case 2:
                 data=ab.nextInt();
                heap.remove(data);
                break;
                case 3:
                System.out.println(heap.peek());
                break;
            }
        }
    }
}

0 Comments:

Post a Comment

Stats