Leetcode 1710. Maximum Units on a Truck

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1710. Maximum Units on a Truck相关的知识,希望对你有一定的参考价值。

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Maximum Units on a Truck

2. Solution

**解析:**Version 1,贪心算法,优先往车上放装东西多的盒子。

  • Version 1
class 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

  1. https://leetcode.com/problems/maximum-units-on-a-truck/

以上是关于Leetcode 1710. Maximum Units on a Truck的主要内容,如果未能解决你的问题,请参考以下文章

1710. Maximum Units on a Truck

1710. Maximum Units on a Truck (E)

[Leetcode] Maximum Subarray

[LeetCode]Maximum Subarray

#Leetcode# 53. Maximum Subarray

LeetCode 53. Maximum Subarray