Sort by

recency

|

421 Discussions

|

  • + 0 comments

    I learned from Google AI, but I think I improved upon it :)

    import heapq

    myheap = [] heapq.heapify(myheap)

    Q = int(input()) valid = set()

    for _ in range(Q):

    command = [int(i) for i in input().split(' ')]
    
    if command[0] == 1:
        heapq.heappush(myheap, command[1])
        valid.add(command[1])
    elif command[0] == 2:
        valid.remove(command[1])
    elif command[0] == 3:
    
        while True:
            ans = myheap[0]
            if ans in valid:
                print(ans)
                break
            else:
                heapq.heappop(myheap)
    
  • + 0 comments

    Are we supposed to implement the heap ourselves or can we use Python's built-in heapq for the heap?

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        static PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        public static void insert(int data){
            minHeap.add(data);
        }
        public static void delete(int data){
            minHeap.remove(data);
        }
        public static void printMin(){
            System.out.println(minHeap.peek());
        }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            for(int i = 0;i<n;i++){
                int action = sc.nextInt();
                if(action == 1){
                    int data = sc.nextInt();
                    insert(data);
                }
                else if(action == 2){
                    int data = sc.nextInt();
                    delete(data);
                }
                else if(action == 3){
                    printMin();
                }
            }
        }
    }
    
  • + 1 comment

    How is this easy lol

  • + 0 comments

    Try this One:- python3-

    from sys import stdin from heapq import heappush, heappop

    heap = [] # Min-heap item_lookup = set() # Set to track valid elements

    def push(v): heappush(heap, v) item_lookup.add(v)

    def discard(v): item_lookup.discard(v)

    def print_min(): # Remove elements from heap that are no longer valid while heap[0] not in item_lookup: heappop(heap)

    print(heap[0])
    

    cmds = { 1: push, 2: discard, 3: print_min }

    n = int(stdin.readline()) for _ in range(n): data = list(map(int, stdin.readline().split(" "))) cmdsdata[0]