storage boxes

 avatar
user_8886827
python
a month ago
1.1 kB
1
Indexable
Never
import heapq
from collections import deque

def storage_capacity(box_heights, shelf_heights):
    result = [-1]*len(shelf_heights)
    shelves = [(val, idx) for idx, val in enumerate(shelf_heights)]
    heapq.heapify(shelves)
    box_idx, result_idx = 0, len(shelves)-1
    while shelves and result_idx>=0:
        current_box = box_heights[box_idx]
        min_shelf_height, min_shelf_idx = heapq.heappop(shelves)
        if current_box<=min_shelf_height:
            while shelf_heights[result_idx]<current_box:
                result_idx-=1
            result[result_idx]=min_shelf_height
            if result_idx==0: 
                return result
            box_idx+=1
        result_idx-=1
    return result
    
shelf_heights = [3, 4, 2, 3]   
box_heights = [2, 3, 3, 4, 5]
print(storage_capacity(box_heights, shelf_heights)) ## (box2,box1,empty,box0) - [3, 3, -1, 2]
box_heights = [3, 3, 3]
print(storage_capacity(box_heights, shelf_heights)) # [3, 3, -1, -1]
box_heights = [8, 3, 3]
print(storage_capacity(box_heights, shelf_heights)) # [-1, -1, -1, -1]
Leave a Comment