Sort by

recency

|

417 Discussions

|

  • + 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());
                }
            }
        }
    }
    
  • + 0 comments

    Solution using 2 heapify mehods and maintaining a heap through vector

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    //used to bubble up the newly added element 
    void heapifyUp(vector<long long int>& heap) {
        int index = heap.size()-1;
        int parent = (index-1)/2;
        while (parent >= 0) {
            if(heap[parent] > heap[index]) {
                swap(heap[parent], heap[index]);
                index = parent;
                parent = (index-1)/2;
            }
            else break;
        }
    }
    
    //used to structure the heap after element deletion
    void heapifyDown(vector<long long int>& heap, long long int index = 0) {
        int left = 2 * index + 1;
        int right = 2 * index + 2;
        int parent = index;
        
        if(left < heap.size() && heap[parent] > heap[left]) 
            parent = left;
        if(right < heap.size() && heap[parent] > heap[right]) 
            parent = right;
            
        if(parent != index) {
            swap(heap[index], heap[parent]);
            heapifyDown(heap, parent);
        } 
    }
    
    void insertToHeap(vector<long long int>& heap, long long int element) {
        heap.push_back(element);
        heapifyUp(heap);
    }
    
    void deleteFromHeap(vector<long long int>& heap, long long int element) {
        int elementIndex = -1;
        for(int i=0; i<heap.size(); i++) {
            if(heap[i] == element){
                elementIndex = i;
            }
        }
        swap(heap[elementIndex], heap[heap.size()-1]);
        heap.resize(heap.size()-1);
        
        //problem we were facing 
        /*
        Fix Heapification After Deletion:
        1.  After removing an element by swapping it with the last element, instead of just calling heapifyDown, you should check whether the element needs to be "bubbled up" or "bubbled down."
        2. If the element that was swapped into the position is smaller than its parent, you should call heapifyUp.
        3. If it's larger than its children, you should call heapifyDown.
    */
        if(elementIndex > 0 && heap[elementIndex] < heap[(elementIndex-1) / 2]) {
            heapifyUp(heap);
        }
        else 
            heapifyDown(heap, elementIndex);
    }
    
    void print(vector<long long int> heap) {
            for(int i:heap) 
        cout<<i<<' ';
        cout<<endl;
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        vector<long long int> heap;
        long long int cases = -1, querry, element = -1;
        cin>>cases;
        
        while(cases--) {
            cin>>querry;
            switch (querry) {
                case 1:
                    cin>>element;
                    insertToHeap(heap, element);
                    break;
                case 2:
                    cin>>element;
                    deleteFromHeap(heap, element);
                    break;
                case 3:
                    if(heap.size() > 0)
                        cout<<heap[0]<<endl;
                    break;
            }
        }
    
        return 0;
    }
    
  • + 0 comments

    For all those who are having trouble with the delete operation, I hope the following link can be helpful; https://en.wikipedia.org/wiki/Binary_heap#Delete