> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-29-2022.md).

# 01/29/2022

Brutal force

During each iteration, found the box with maximum units, computer the units, and get the left trucksize until the trucksize is 0.

Time O(N^2)

Space O(1)

Sort the 2d array by the units in decreasing order. During the for loop, set the boxCount = min of truckSize and numberOfBoxes. Then compute the count and new left truckSize until the truckSize = 0.

Time O(NLOGN)

Space O(1)

```
class Solution { 
    public int maximumUnits(int[][] boxTypes, int truckSize) { 
    Arrays.sort(boxTypes, (a,b) -> b[1] - a[1]); 
    int count = 0;
    
    for(int i = 0; i < boxTypes.length; i++){
        
       int boxCount = Math.min(truckSize, boxTypes[i][0]);
       count = count + boxCount * boxTypes[i][1];
       truckSize = truckSize - boxTypes[i][0];
        
        if (truckSize == 0) break;
                   
        
    }
    return count;
    
 }


 
}

/////
class Solution { 
    public int maximumUnits(int[][] boxTypes, int truckSize) { 
        Arrays.sort(boxTypes, (a, b) -> b[1] - a[1]); 
        int unitCount = 0; 
        for (int[] boxType : boxTypes) { 
            int boxCount = Math.min(truckSize, boxType[0]); 
            unitCount += boxCount * boxType[1]; 
            truckSize -= boxCount; 
            if (truckSize == 0) 
                break; 
        }
         return unitCount;
     } 
}
```
