简体   繁体   中英

Finding the maximum number of units a truck can be filled with given its box size

This problem is a variation of the LeetCode 1710 (easy) problem. I was wondering what my time and space complexity here would be for my solution, and if there are better approaches to solve this question.

Question: We want to find the maximum number of units of a product a truck can hold depending on how many boxes it can hold (given by truckSize).

Variables (with example):

  • num = 3 (number of products)
  • boxes = [1,2,3] (list of integers of number of boxes for each product)
  • unitSize = 3 (just represents the size of unitsPerBox list)
  • unitsPerBox = [3,2,1] (list of integers representing the number of units packed in each box)
  • truckSize = 3 (the number of boxes the truck can carry).

So in this example, the truck will be able to hold a maximum of 7 units because

Product 0 = 1 box with 3 units -> [3]
Product 1 = 2 boxes with 2 units -> [2] [2]
Product 2 = 3 boxes with 1 unit -> [1] [1] [1]

Since the truck can hold 3 boxes, we would maximize units by adding boxes [3] + [2] + [2] = 7 units.

Here is my code, I am calculating all possible boxes using a greedy solution. packedList is the list I use which contains units of all boxes for all products. I sort it and sum it up depending on truckSize to get the final answer.

def getMaxUnit(num, boxes, unitSize, unitsPerBox, truckSize)  

    packedList = []
    maxUnits = 0
    
    for i in range(num):
        for j in range(boxes[i]):
            packedList.insert(len(packedList), unitsPerBox[i])
        
    packedList.sort(reverse = True)
    
    # Edge case - if truckSize is bigger, then
    # we can use every box in packedList
    if truckSize > len(packedList):
        for i in range(len(packedList)):
            maxUnits += packedList[i]        
    else:
        # Since list is sorted, get the max number of units
        # from packedList
        for i in range(truckSize):
            maxUnits += packedList[i]
    
    return(maxUnits)

Les's say,

  • X = Number of products
  • Y = Maximum number of boxes for each product
  • Z = X * Y

Assuming ( truckSize < Z ), the complexity of your code would be: O(Z log(Z)) . The space complexity of your code would be: O(Z) . The complexity is such because you first build packedList and sort it.

I think you can further improve the time and space complexity of your code. You can build a list of tuples, where each tuples will contain <number-of-units, number-of-boxes> . For your sample input, it would be like this:

[(3, 1), (2, 2), (1, 3)]

Then sort this list on decreasing order of the number of units (first item of the tuple). As this problem asked to maximize the number of units in the loaded truck. Then, you can make greedy choice by selecting boxes which have large number of units. Choose the boxes in a way so that it will be under the truckSize constraint.

The size of the list of tuples would be O(X) . Then sorting this list would cost O(X log(X)) . By this way you can reduce the time and space complexity of this problem as X is clearly less than Z .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM