QHEAP1

Sort by

recency

|

41 Discussions

|

  • + 0 comments

    I believe this test case is wrong. In order to print the final number 20842598 the input needs one more line with content '3'. Otherwise the number is never printed and the case fails.

    22 1 286789035 1 255653921 1 274310529 1 494521015 3 2 255653921 2 286789035 3 1 236295092 1 254828111 2 254828111 1 465995753 1 85886315 1 7959587 1 20842598 2 7959587 3 1 -51159108 3 2 -51159108 3 1 789534713

  • + 0 comments

    from heapq import heappush, heappop h= [] p = set()

    data = [input().split(' ') for _ in range(int(input()))] for idx in range(len(data)): records = data[idx] if records[0] == '1': # p.add(records[1],heappush(h, records[1])) p.add(int(records[1])) heappush(h, int(records[1])) elif records[0] == '2': p.discard(int(records[1])) elif records[0] == '3': while h[0] not in p: heappop(h) print(h[0])

  • + 0 comments
    from heapq import heappush, heappop
    
    h, p = [], set()
    for s in [input().split(' ') for _ in range(int(input()))]:
        if s[0] == '1':
            p.add(int(s[1])), heappush(h, int(s[1]))
        elif s[0] == '2':
            p.discard(int(s[1]))
        elif s[0] == '3':
            while h[0] not in p:
                heappop(h)
            print(h[0])
    
  • + 0 comments

    Here is hackerrank QHEAP1 problem solution in Python, Java, C++, C and javascript

  • + 0 comments

    In JAVA8 :

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            PriorityQueue<Integer> minHeap = new PriorityQueue<>(); 
            int i = 0, Q = Integer.parseInt(br.readLine().trim());
            while (i++ < Q) {
                String[] input = br.readLine().split(" ");
                switch(Integer.parseInt(input[0])){
                    case 1 :{
                        minHeap.offer(Integer.parseInt(input[1]));
                        break;
                    }
                    case 2 :{
                        minHeap.remove(Integer.parseInt(input[1]));
                        break;
                    }
                    case 3 :{
                        System.out.println(minHeap.peek());
                        break;
                    }
                    default :{
                        System.out.println("Invalid !");
                    }
                }
            }
        }
    }