Problem Solving
83. Maximum Units on a Truck
굥깡
2023. 1. 15. 21:05
728x90
https://leetcode.com/problems/maximum-units-on-a-truck/description/
Maximum Units on a Truck - LeetCode
Maximum Units on a Truck - You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]: * numberOfBoxesi is the number of boxes of type i. * numberOfUnitsPerBoxi
leetcode.com
[박수 개수, 박스 크기]의 리스트와 넣을 수 있는 박수 개수를 입력받아 선택된 박스 크기 합의 최댓값을 구하는 문제
class Solution:
def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
boxTypes.sort(key=lambda x:x[1], reverse=True)
print(boxTypes)
answer = 0
while truckSize > 0:
for i in boxTypes:
if i[0] <= truckSize:
answer = answer + i[0]*i[1]
truckSize = truckSize - i[0]
else:
answer = answer + truckSize*i[1]
break
return answer
박스 개수가 먼저인지 크기가 먼저인지 헷갈렸음
문제 푸는 건 오래 안 걸림