Maximum Units on a Truck

ㅋㅋ·2022년 7월 1일
0

알고리즘-leetcode

목록 보기
22/135

박스 정보가 담긴 벡터와 트럭의 크기를 받는다.

박스 정보에는 사용할 수 있는 박스의 개수와 Unit이라는 일종의 점수가 있다.

트럭 크기 1당 박스 1개를 실을 수 있을 때, Unit의 최대값을 구하는 문제이다.

class Solution {
public:
    int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
        
        sort(boxTypes.begin(), boxTypes.end(), 
            [] (const auto &lhs, const auto &rhs)
            {
                return (lhs[1] > rhs[1]);
            });
        
        int sum{0};
        for (auto &boxType : boxTypes)
        {
            if (truckSize < boxType[0])
            {
                sum += truckSize * boxType[1];
                break;
            }
            else
            {
                sum += boxType[0] * boxType[1];
                truckSize -= boxType[0];
            }
            
        }
        
        return sum;
    }
};

0개의 댓글