[LeetCode] 1710. Maximum Units on a Truck

Chobby·2025년 10월 10일
1

LeetCode

목록 보기
584/650

😎풀이

  1. 한 박스에 적재 가능한 물량을 기준으로 내림차 순 정렬
  2. truckSize에 초과되지 않는 선에서 모든 박스 적재
  3. 적재된 물량 반환환
function maximumUnits(boxTypes: number[][], truckSize: number): number {
    let sumUnits = 0
    let truckRemain = truckSize
    const sortedBoxTypes = boxTypes.toSorted((a, b) => b[1] - a[1])
    for(const [boxCount, unitPerBox] of sortedBoxTypes) {
        for(let i = 0; i < boxCount; i++) {
            if(truckRemain === 0) return sumUnits
            sumUnits += unitPerBox
            truckRemain--
        }
    }
    return sumUnits
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글