Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 51d4f31

Browse files
committed
Add solution #1710
1 parent 7a843f1 commit 51d4f31

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,492 LeetCode solutions in JavaScript
1+
# 1,493 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1317,6 +1317,7 @@
13171317
1705|[Maximum Number of Eaten Apples](./solutions/1705-maximum-number-of-eaten-apples.js)|Medium|
13181318
1706|[Where Will the Ball Fall](./solutions/1706-where-will-the-ball-fall.js)|Medium|
13191319
1707|[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|
13201321
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13211322
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13221323
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
};

0 commit comments

Comments
 (0)