File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 1- # 1,492 LeetCode solutions in JavaScript
1+ # 1,493 LeetCode solutions in JavaScript
22
33[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
44
131713171705|[ Maximum Number of Eaten Apples] ( ./solutions/1705-maximum-number-of-eaten-apples.js ) |Medium|
131813181706|[ Where Will the Ball Fall] ( ./solutions/1706-where-will-the-ball-fall.js ) |Medium|
131913191707|[ Maximum XOR With an Element From Array] ( ./solutions/1707-maximum-xor-with-an-element-from-array.js ) |Hard|
1320+ 1710|[ Maximum Units on a Truck] ( ./solutions/1710-maximum-units-on-a-truck.js ) |Easy|
132013211716|[ Calculate Money in Leetcode Bank] ( ./solutions/1716-calculate-money-in-leetcode-bank.js ) |Easy|
132113221718|[ Construct the Lexicographically Largest Valid Sequence] ( ./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js ) |Medium|
132213231726|[ Tuple with Same Product] ( ./solutions/1726-tuple-with-same-product.js ) |Medium|
Original file line number Diff line number Diff line change 1+ /**
2+ * 1710. Maximum Units on a Truck
3+ * https://leetcode.com/problems/maximum-units-on-a-truck/
4+ * Difficulty: Easy
5+ *
6+ * You are assigned to put some amount of boxes onto one truck. You are given a 2D array
7+ * boxTypes, where boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]:
8+ * - numberOfBoxesi is the number of boxes of type i.
9+ * - numberOfUnitsPerBoxi is the number of units in each box of the type i.
10+ *
11+ * You are also given an integer truckSize, which is the maximum number of boxes that can be
12+ * put on the truck. You can choose any boxes to put on the truck as long as the number of
13+ * boxes does not exceed truckSize.
14+ *
15+ * Return the maximum total number of units that can be put on the truck.
16+ */
17+
18+ /**
19+ * @param {number[][] } boxTypes
20+ * @param {number } truckSize
21+ * @return {number }
22+ */
23+ var maximumUnits = function ( boxTypes , truckSize ) {
24+ boxTypes . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] ) ;
25+ let result = 0 ;
26+ let remainingBoxes = truckSize ;
27+
28+ for ( const [ count , units ] of boxTypes ) {
29+ const boxesToTake = Math . min ( count , remainingBoxes ) ;
30+ result += boxesToTake * units ;
31+ remainingBoxes -= boxesToTake ;
32+ if ( remainingBoxes === 0 ) break ;
33+ }
34+
35+ return result ;
36+ } ;
You can’t perform that action at this time.
0 commit comments