• + 0 comments

    Guys, I implemented the whole MinHeap and stuff:

    class MinHeap:
        def __init__(self, arr=[]):
            self.heap = arr[:]
            self.convert_to_heap()
    
        def heapify_up(self, index):
            parent = (index - 1) // 2
            while index > 0 and self.heap[index] < self.heap[parent]:            
                self.heap[index], self.heap[parent] = self.heap[parent], self.heap[index]            
                index = parent
                parent = (index - 1) // 2
    
        def heapify_down(self, index):
            l = len(self.heap)
            while True:
                current = index
                left = (2 * index) + 1
                right = (2 * index) + 2
    
                # Check that left and right indexes are lesser than the heap's size and, therefore, are able to access its elements. Else, an index out of range error will be thrown.
                if left < l and self.heap[left] < self.heap[current]:
                    current = left
                if right < l and self.heap[right] < self.heap[current]:
                    current = right
                # In case that any value computed for the left or right indexes, which correspond to the indexes of current's children, is greater than the heap's size:
                # * It means that current's value, which hwas updated in the prior iteration, corresponds to a leaft
                # * Index's value will not be updated and, therefore, will equal current's value, which was set in the prior iteration.
                if current == index:
                    break
    
                self.heap[index], self.heap[current] = self.heap[current], self.heap[index]
                index = current
    
        def convert_to_heap(self):
            for i in range((len(self.heap) // 2 ) - 1, -1, -1):
                self.heapify_down(i)
    
        def insert(self, val):
            self.heap.append(val)
            self.heapify_up(len(self.heap) - 1)
    
        def pop_root(self):
            if not self.heap:
                return None
            root = self.heap[0]
            last = self.heap.pop()
            if self.heap:
                self.heap[0] = last
                self.heapify_down(0)
            return root
        
        def delete_value(self, val):
            if val not in self.heap:
                return
            
            index = self.heap.index(val)
            last = self.heap.pop()
    
            if index < len(self.heap):
                self.heap[index] = last
                if index > 0 and self.heap[index] < self.heap[(index - 1) // 2]:
                    self.heapify_up(index)
                else:
                    self.heapify_down(index)
    
        def get_root(self):
            return self.heap[0]
    
    def cookies(k, A):
        heap = MinHeap(A)
        count = 0
        
        print(f"h = {heap.heap}")
        while len(heap.heap) > 1 and heap.get_root() < k:
            flsc = heap.pop_root()
            slsc = heap.pop_root()
            print(f"r1 = {flsc}, r1 = {slsc}")
    
            if slsc is None:  # Si no hay una segunda galleta, no se puede continuar
                return -1
    
            new_cookie = flsc + (2 * slsc)
            heap.insert(new_cookie)
            count += 1
            print(f"heap = {heap.heap}")
    
        return count if heap.get_root() is not None and heap.get_root() >= k else -1
    

    However, test case 22 was throwing a TLE, so I cahnged the whole code for this instead:

    import heapq
    
    def cookies(k, A):
        heapq.heapify(A)
        count = 0
        
        print(f"h = {heap.heap}")
        while len(A) > 1 and A[0] < k:
            flsc = heapq.heappop(A)
            slsc = heapq.heappop(A)
    
            heapq.heappush(A, flsc + (2 * slsc))
            count += 1
    
        return count if A[0] >= k else -1