diff options
author | Jack Sangdahl <[email protected]> | 2025-08-06 11:53:26 +0200 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-08-06 11:53:26 +0200 |
commit | c6c6abdc55f6dc40c411193e97aaea13103727cd (patch) | |
tree | d8690f65e12dac27b764c5d555c85936ac48a325 /fruit_into_baskets3.py | |
parent | 9cee92d538c64f43a3dfc1b25675e006e2ee7b91 (diff) | |
download | leetcode-c6c6abdc55f6dc40c411193e97aaea13103727cd.tar.gz leetcode-c6c6abdc55f6dc40c411193e97aaea13103727cd.zip |
3479. fruits into baskets 3
Diffstat (limited to 'fruit_into_baskets3.py')
-rw-r--r-- | fruit_into_baskets3.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/fruit_into_baskets3.py b/fruit_into_baskets3.py new file mode 100644 index 0000000..3cdf123 --- /dev/null +++ b/fruit_into_baskets3.py @@ -0,0 +1,50 @@ +# 3479. Fruits Into Baskets III + +""" +you are given two arrays of integers, 'fruits' and 'baskets', each of length +'n', where 'fruits[i]' represents the quantity of the i'th type of fruit, and +'baskets[j]' represents the capacity of the j'th basket. from left to right, +place the fruits according to these rules: each fruit type must be placed in +the leftmost available basket with a capacity greater than equal to the +quantity of that fruit type. return the number of fruit types that remain +unplaced after all possible allocations are made. +""" + + +class Solution(object): + def numOfUnplacedFruits(self, fruits, baskets): + """ + :type fruits: List[int] + :type baskets: List[int] + :rtype: int + """ + n, m = len(baskets), 1 + while m <= n: + m <<= 1 + tree = [0] * (2 * m) + for i in range(n): + tree[m + i] = baskets[i] + for i in range(m - 1, 0, -1): + tree[i] = max(tree[2 * i], tree[2 * i + 1]) + ans = 0 + for i in fruits: + curr = 1 + if tree[curr] < i: + ans += 1 + continue + while curr < m: + if tree[2 * curr] >= i: + curr *= 2 + else: + curr = 2 * curr + 1 + tree[curr] = -1 + while curr > 1: + curr //= 2 + tree[curr] = max(tree[2 * curr], tree[2 * curr + 1]) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.numOfUnplacedFruits(fruits=[4, 2, 5], baskets=[3, 5, 4])) + print(obj.numOfUnplacedFruits(fruits=[3, 6, 1], baskets=[6, 4, 7])) |