【每日一题Day28】LC1710卡车上的最大单元数 | 贪心

简介: 整数 truckSize 表示卡车上可以装载 箱子 的 最大数量 。只要箱子数量不超过 truckSize ,你就可以选择任意箱子装到卡车上。

卡车上的最大单元数【LC1710】


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.


请你将一些箱子装在 一辆卡车 上。给你一个二维数组 boxTypes ,其中 boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi] :


  • numberOfBoxesi 是类型 i 的箱子的数量。


  • numberOfUnitsPerBoxi 是类型 i 每个箱子可以装载的单元数量。


整数 truckSize 表示卡车上可以装载 箱子最大数量 。只要箱子数量不超过 truckSize ,你就可以选择任意箱子装到卡车上。


返回卡车可以装载 单元 最大 总数*。*


又又又崩了,好点我做完了=)


  • 思路:贪心


。局部最优:尽可能装载容量大的箱子,即将箱子按照可以装载的单元数量从大到小装到卡车上


。全局最优:箱子数量一定时,能够装载的单元最大


  • 实现


。先将箱子按照容量从大到小排序


。依次遍历排序后的箱子,并使用变量统计已装载的单元数量res和箱子数量count


  • 如果count+当前箱子数量 boxTypes[i][0] > 卡车容量trunkSize,那么只装载卡车容量-count个箱子,res += (trunkSize - count) * boxTypes\[i][1] 并退出循环


  • 如果count+当前箱子数量boxTypes[i][0] > 卡车容量trunkSize,那么只装载boxTypes[i][0] 个箱子,res += boxTypes\[i][0] * boxTypes\[i][1]


  • 代码


class Solution {
    public int maximumUnits(int[][] boxTypes, int truckSize) {
      Arrays.sort(boxTypes,new Comparator<int[]>(){
          public int compare(int[] box1, int[] box2){
              return box2[1] - box1[1];
          }
      });
        int res = 0;
        int count = 0;
        for (int i = 0; i < boxTypes.length; i++){
            if (count + boxTypes[i][0] > truckSize){
                res += (truckSize - count) * boxTypes[i][1];
                break;
            }else{
                res += boxTypes[i][0] * boxTypes[i][1]; 
                count += boxTypes[i][0];
            }
        }
        return res;
    }
}


  • 复杂度


。时间复杂度:O ( n l o g n ),排序需要用O ( n l o g n ) 的时间

。空间复杂度:O ( 1 ),排序需要用O ( n l o g n ) 的递归调用栈空间

目录
相关文章
|
4月前
【每日一题Day243】LC1595连通两组点的最小成本 | 状态压缩dp
【每日一题Day243】LC1595连通两组点的最小成本 | 状态压缩dp
26 0
|
4月前
|
算法
【每日一题Day363】LC275H 指数Ⅱ | 二分答案
【每日一题Day363】LC275H 指数Ⅱ | 二分答案
34 0
|
4月前
【每日一题Day297】LC1444切披萨的方案数 | 动态规划+二维前缀和
【每日一题Day297】LC1444切披萨的方案数 | 动态规划+二维前缀和
26 0
|
4月前
|
机器学习/深度学习
【每日一题Day125】LC1326灌溉花园的最少水龙头数目 | 动态规划 贪心
【每日一题Day125】LC1326灌溉花园的最少水龙头数目 | 动态规划 贪心
30 0
|
4月前
【每日一题Day342】LC2578最小和分割 | 贪心
【每日一题Day342】LC2578最小和分割 | 贪心
18 0
|
4月前
【每日一题Day144】LC1617统计子树中城市之间最大距离 | 树形dp
【每日一题Day144】LC1617统计子树中城市之间最大距离 | 树形dp
19 0
|
4月前
【每日一题Day314】LC1921消灭怪物的最大数量 | 贪心+排序
【每日一题Day314】LC1921消灭怪物的最大数量 | 贪心+排序
21 0
|
4月前
【每日一题Day360】LC1465切割后面积最大的蛋糕 | 贪心
【每日一题Day360】LC1465切割后面积最大的蛋糕 | 贪心
28 0
|
4月前
【每日一题Day241】LC1254统计封闭岛屿的数目 | dfs
【每日一题Day241】LC1254统计封闭岛屿的数目 | dfs
21 1
|
4月前
【每日一题Day257】LC2178拆分成最多数目的正偶数之和 | 贪心
【每日一题Day257】LC2178拆分成最多数目的正偶数之和 | 贪心
19 2