From e5dcce6b8d7490d976e4722108d300e174380470 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sat, 2 Jul 2022 01:05:59 +0800 Subject: [PATCH] add: 220701 --- cpp/2207/220701.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cpp/2207/220701.cpp diff --git a/cpp/2207/220701.cpp b/cpp/2207/220701.cpp new file mode 100644 index 0000000..5360a18 --- /dev/null +++ b/cpp/2207/220701.cpp @@ -0,0 +1,28 @@ +#include +#include + +/** + * 1710. 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 is the number of units in each box of the type i. + * You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize. + * Return the maximum total number of units that can be put on the truck. + */ + +class Solution { +public: + int maximumUnits(std::vector>& boxTypes, int truckSize) { + std::sort(boxTypes.begin(), boxTypes.end(), [](const auto& x, const auto& y) { + return x[1] > y[1]; + }); + int ans = 0; + for (const auto& i : boxTypes) { + ans += i[1] * std::min(truckSize, i[0]); + truckSize -= i[0]; + if (truckSize <= 0) + break; + } + return ans; + } +};