1889. Minimum Space Wasted From Packaging

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1889

class Solution:
    def minWastedSpace(self, packages: List[int], boxes: List[List[int]]) -> int:
        mod = 10**9 + 7
        res = inf
        packages.sort()
        for box in boxes:
            box.sort()
            if packages[-1] > box[-1]:
                continue
            s = i = 0
            for b in box:
                j = bisect_right(packages, b, lo=i)
                s += (j - i) * b
                i = j
            res = min(res, s)
        if res == inf:
            return -1
        return (res - sum(packages)) % mod