QHEAP1

Sort by

recency

|

13 Discussions

|

  • + 0 comments

    Python. Possibly not the intended solution.

    import heapq
    
    q = int(input())
    heap = []
    deleted = set()
    for _ in range(q):
        line = input().split()
        command = line[0]
        if len(line) > 1:
            val = int(line[1])
        if command == "1":
            if val in deleted:
                deleted.remove(val)
            else:
                heapq.heappush(heap, val)
        elif command == "2":
            deleted.add(val)
        elif command == "3":
            x = heap[0]
            while x in deleted:
                heapq.heappop(heap)
                deleted.remove(x)
                x = heap[0]
            print(x)
    
  • + 0 comments

    Java O(Q log n)

     public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int Q = scanner.nextInt();
            
            PriorityQueue<Integer> heap = new PriorityQueue<>();
    
            for (int i = 0; i < Q; i++) {
                int queryType = scanner.nextInt();
    
                switch (queryType) {
                    case 1:
                        int elementToAdd = scanner.nextInt();
                        heap.add(elementToAdd);
                        break;
                    case 2:
                        int elementToDelete = scanner.nextInt();
                        heap.remove(elementToDelete);
                        break;
                    case 3:
                        System.out.println(heap.peek());
                        break;
                }
            }
            scanner.close();
        }
    
  • + 0 comments

    C#

    public class MinHeap {
            private HashSet<int> _lookup = new HashSet<int>();
            private List<int> _heap = new List<int>();
            private bool HasParent(int i) => (i - 1) / 2 < _heap.Count();
            private int GetParentIndex(int i) => (i - 1) / 2;
            private int GetParent(int i) => _heap[GetParentIndex(i)];
            
            private bool HasLeftChild(int i) => 2 * i + 1 < _heap.Count();
            private int GetLeftChildIndex(int i) => 2 * i + 1;
            private int GetLeftChild(int i) => _heap[GetLeftChildIndex(i)];
            
            private bool HasRightChild(int i) => 2 * i + 2 < _heap.Count();
            private int GetRightChildIndex(int i) => 2 * i + 2;
            private int GetRightChild(int i) => _heap[GetRightChildIndex(i)];
            
            public void Print() { 
                Delete();
                Console.WriteLine(_heap[0]);
            }
            
            public void Add(int num) {
                _heap.Add(num);
                _lookup.Add(num);
                int lindex = _heap.Count() - 1;
                while (lindex >= 0 && HasParent(lindex) && num < GetParent(lindex)) {
                    // bubbling up...
                    var temp = _heap[lindex];
                    _heap[lindex] = _heap[GetParentIndex(lindex)];
                    _heap[GetParentIndex(lindex)] = temp;
                    lindex = GetParentIndex(lindex);
                }
            }
            
            public void Delete(int num) =>
                _lookup.Remove(num);
            
            private void Delete() {
                while (_heap.Count() > 0 && _lookup.Count() > 0 && !_lookup.Contains(_heap[0])) {
                    int index = 0;
                    _heap[index] = _heap[_heap.Count()-1];
                    _heap.RemoveAt(_heap.Count()-1);
                    if (index == _heap.Count()-1) {
                        return;
                    }
                    
                    int num = _heap[index];
                    int temp = 0;
                    while ((HasLeftChild(index) && GetLeftChild(index) < num) 
                        || (HasRightChild(index) && GetRightChild(index) < num)) {
                        if (HasLeftChild(index) && num > GetLeftChild(index)
                            && (!HasRightChild(index) || GetLeftChild(index) < GetRightChild(index))) {
                            temp = _heap[GetLeftChildIndex(index)];
                            _heap[GetLeftChildIndex(index)] = _heap[index];
                            _heap[index] = temp;
                            index = GetLeftChildIndex(index);
                        } else if (HasRightChild(index) && num > GetRightChild(index)
                            && GetLeftChild(index) > GetRightChild(index)) {
                            temp = _heap[GetRightChildIndex(index)];
                            _heap[GetRightChildIndex(index)] = _heap[index];
                            _heap[index] = temp;
                            index = GetRightChildIndex(index);
                        }
                    }
                }
            }
        }
    
  • + 0 comments

    idk why is problem is described as "help you get a better understanding of basic heap operations. " but the best solution for this problem is just a fixed array of ints

  • + 0 comments

    Javascript

      let queries=input.split('\n')
        queries.splice(0,1)
        let arr=[]
        let min=null;
        for(let query of queries){
            query=query.split(' ')
            let op=query[0]
            let param=query[1]
            if(op=='1'){
                arr.push(param)            
                if(parseFloat(param)<parseFloat(min)){
                    min=param
                }else if(min===null){
                    min=param
                }
            }
            if(op=='3'){
                console.log(min)
            }
            if(op=='2'){
                arr.splice(arr.indexOf(param),1)     
                if(min==param){
                    min=Math.min(...arr)
                }
            }
        }