We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
By looking at some comment, it seems like no one actually tried to use priority queue to mimic the min heap? So many ppl used treeset but I am not sure which one is actually the right one to use. After checking this StackOverflow, I decided to use priority queue http://stackoverflow.com/questions/14165325/is-there-a-heap-in-java
Also, I don't want to bother implementing heap from the scratch...
publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerscan=newScanner(System.in);intn=scan.nextInt();PriorityQueue<Integer>minHeap=newPriorityQueue<Integer>(n,(i1,i2)->i1.compareTo(i2));/* PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(n, new Comparator<Integer>( int compare(Integer i1, Integer i2){ return i1.compareTo(i2); })); */for(inti=0;i<n;i++){inttype=scan.nextInt();switch(type){case1:intdata1=scan.nextInt();minHeap.offer(data1);break;case2:intdata2=scan.nextInt();minHeap.remove(data2);break;case3:System.out.println(minHeap.peek());break;default:break;}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
QHEAP1
You are viewing a single comment's thread. Return to all comments →
By looking at some comment, it seems like no one actually tried to use priority queue to mimic the min heap? So many ppl used treeset but I am not sure which one is actually the right one to use. After checking this StackOverflow, I decided to use priority queue http://stackoverflow.com/questions/14165325/is-there-a-heap-in-java Also, I don't want to bother implementing heap from the scratch...