/* * @LastEditTime: 2025-12-24 13:32:26 */ classSolution { public: intminimumBoxes(vector<int>& apple, vector<int>& capacity){ sort(capacity.begin(), capacity.end(), greater<>()); int cnt = 0; for (int t : apple) { cnt += t; } int ans = 0; for (int t : capacity) { cnt -= t; ans++; if (cnt <= 0) { return ans; } } return-1; // Fake Return } };
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
''' LastEditTime: 2025-12-24 13:40:50 ''' from typing importList
classSolution: defminimumBoxes(self, apple: List[int], capacity: List[int]) -> int: cnt = sum(apple) ans = 0 for t insorted(capacity, reverse=True): cnt -= t ans += 1 if cnt <= 0: return ans
# if __name__ == "__main__": # sol = Solution() # print(sol.minimumBoxes([1,3,2], [4,3,1,5,2]))
/* * @LastEditTime: 2025-12-24 13:45:02 */ package main
import"sort"
funcminimumBoxes(apple []int, capacity []int)int { cnt := 0 for _, t := range apple { cnt += t } sort.Sort(sort.Reverse(sort.IntSlice(capacity))) for i, t := range capacity { cnt -= t if cnt <= 0 { return i + 1 } } return-1// Fake Return }