Leetcode 1710. Maximum Units on a Truck | | Leetcode 1710. Maximum Units on a Truck 文章作者:Tyan博客:noahsnail.com | CSDN | 简书fr 1. Description 2. Solution解析:Version 1,贪心算法,优先往车上放装东西多的盒子。 Version 1 1234567891011121314class Solution: def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int: count = 0 boxTypes.sort(key=lambda x: x[1], reverse=True) i = 0 while truckSize > 0 and i < len(boxTypes): if truckSize - boxTypes[i][0] > 0: truckSize -= boxTypes[i][0] count += boxTypes[i][0] * boxTypes[i][1] else: count += truckSize * boxTypes[i][1] truckSize = 0 i += 1 return count Reference https://leetcode.com/problems/maximum-units-on-a-truck/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏