Sort by

recency

|

419 Discussions

|

  • + 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]

  • + 0 comments
    #include <bits/stdc++.h>
    #define lli long long int
    using namespace std;
    
    struct greaters {
        bool operator() (const long&a, const long&b) const {
            return a > b;
        }
    };
    
    int main (int argc, char *argv[]) {
    
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
    
        vector<int> v;
    
        lli tc;
        cin >> tc;
        while(tc--) {
            lli x;
            cin >> x;
            if (x == 1) {
                // insert
                lli val;
                cin >> val;
                v.push_back(val);
                push_heap(v.begin(), v.end(), greaters());
            } else if (x == 2) {
                // delete specific node
                lli val;
                cin >> val;
                auto it = find(v.begin(), v.end(), val);
                bool isFound = it != v.end();
                if (isFound) {
                    swap(*it, v.back());
                    v.pop_back();
                    make_heap(v.begin(), v.end(), greaters());
                }
            } else {
                // print minimum heap
                cout << v.front() << "\n";
            }
        }
        
    
        return 0;
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    //Youtube : The Adarsh 1M
    
    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. */
            int n;
            Scanner sc = new Scanner(System.in);
            n = sc.nextInt();
            // int min = 99999;
            // int [] arr = new int[n];
            // List<Integer> arr = new ArrayList<>();
            // Set<Integer> arr = new HashSet<>();
            TreeMap<Integer, Integer> mp = new TreeMap<>();
            for(int i = 0; i < n; i++) {
                int op = sc.nextInt();
                if(op == 1) {
                    int num = sc.nextInt();
                    // arr.add(num);
                    mp.put(num, mp.getOrDefault(num, 0) + 1);
                }
                else if(op == 2){
                    int num = sc.nextInt();
                    // arr.remove(num);
                    if(mp.get(num) > 1){
                        mp.put(num, mp.get(num)-1);
                    }
                    else {
                        mp.remove(num);
                    }
                }
                else {
                    // int min = 9999;
                    // for(int lol : arr){
                    //     min = Math.min(min, lol);
                    // }
                    
                    System.out.println(mp.firstKey());
                }
            }
        }
    }