1710:卡车上的最大单元数
This commit is contained in:
parent
b46b247027
commit
7290d1ec5f
79
src/main/java/leetcode/editor/cn/MaximumUnitsOnATruck.java
Normal file
79
src/main/java/leetcode/editor/cn/MaximumUnitsOnATruck.java
Normal file
@ -0,0 +1,79 @@
|
||||
//请你将一些箱子装在 一辆卡车 上。给你一个二维数组 boxTypes ,其中 boxTypes[i] = [numberOfBoxesi,
|
||||
//numberOfUnitsPerBoxi] :
|
||||
//
|
||||
//
|
||||
// numberOfBoxesi 是类型 i 的箱子的数量。
|
||||
// numberOfUnitsPerBoxi 是类型 i 每个箱子可以装载的单元数量。
|
||||
//
|
||||
//
|
||||
// 整数 truckSize 表示卡车上可以装载 箱子 的 最大数量 。只要箱子数量不超过 truckSize ,你就可以选择任意箱子装到卡车上。
|
||||
//
|
||||
// 返回卡车可以装载 单元 的 最大 总数。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
|
||||
//输出:8
|
||||
//解释:箱子的情况如下:
|
||||
//- 1 个第一类的箱子,里面含 3 个单元。
|
||||
//- 2 个第二类的箱子,每个里面含 2 个单元。
|
||||
//- 3 个第三类的箱子,每个里面含 1 个单元。
|
||||
//可以选择第一类和第二类的所有箱子,以及第三类的一个箱子。
|
||||
//单元总数 = (1 * 3) + (2 * 2) + (1 * 1) = 8
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
|
||||
//输出:91
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= boxTypes.length <= 1000
|
||||
// 1 <= numberOfBoxesi, numberOfUnitsPerBoxi <= 1000
|
||||
// 1 <= truckSize <= 10⁶
|
||||
//
|
||||
// Related Topics 贪心 数组 排序 👍 21 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
//1710:卡车上的最大单元数
|
||||
class MaximumUnitsOnATruck {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new MaximumUnitsOnATruck().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maximumUnits(int[][] boxTypes, int truckSize) {
|
||||
Arrays.sort(boxTypes, Comparator.comparingInt(o -> o[1]));
|
||||
int sum = 0;
|
||||
int index = boxTypes.length - 1;
|
||||
while (truckSize > 0 && index >= 0) {
|
||||
if (boxTypes[index][0] < truckSize) {
|
||||
sum += boxTypes[index][0] * boxTypes[index][1];
|
||||
truckSize -= boxTypes[index][0];
|
||||
index--;
|
||||
} else {
|
||||
sum += truckSize * boxTypes[index][1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<p>请你将一些箱子装在 <strong>一辆卡车</strong> 上。给你一个二维数组 <code>boxTypes</code> ,其中 <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numberOfBoxes<sub>i</sub></code> 是类型 <code>i</code> 的箱子的数量。</li>
|
||||
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>是类型 <code>i</code> 每个箱子可以装载的单元数量。</li>
|
||||
</ul>
|
||||
|
||||
<p>整数 <code>truckSize</code> 表示卡车上可以装载 <strong>箱子</strong> 的 <strong>最大数量</strong> 。只要箱子数量不超过 <code>truckSize</code> ,你就可以选择任意箱子装到卡车上。</p>
|
||||
|
||||
<p>返回卡车可以装载 <strong>单元</strong> 的 <strong>最大</strong> 总数<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>箱子的情况如下:
|
||||
- 1 个第一类的箱子,里面含 3 个单元。
|
||||
- 2 个第二类的箱子,每个里面含 2 个单元。
|
||||
- 3 个第三类的箱子,每个里面含 1 个单元。
|
||||
可以选择第一类和第二类的所有箱子,以及第三类的一个箱子。
|
||||
单元总数 = (1 * 3) + (2 * 2) + (1 * 1) = 8</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
|
||||
<strong>输出:</strong>91
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= boxTypes.length <= 1000</code></li>
|
||||
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
|
||||
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>排序</li></div></div><br><div><li>👍 21</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user