Problem Solving/LeetCode
Today's Challenge
fabichoi
2022. 7. 1. 23:45
https://leetcode.com/problems/maximum-units-on-a-truck/
Maximum Units on a Truck - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Easy 난이도라, 문제가 말하는대로 풀면 된다.
다만 sort 하는게 익숙치 않아서 ㅋㅋㅋㅋ 좀 걸렸음
class Solution:
def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
boxTypes.sort(key=lambda x:(x[1]), reverse=True)
res = 0
for b in boxTypes:
if truckSize - b[0] >= 0:
res += b[1] * b[0]
truckSize -= b[0]
else:
res += b[1] * (truckSize)
break
return res
반응형