1710. 卡车上的最大单元数

地址:

力扣icon-default.png?t=M0H8https://leetcode-cn.com/problems/maximum-units-on-a-truck/

题目:

请你将一些箱子装在 一辆卡车 上。给你一个二维数组 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 <= 106

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-units-on-a-truck
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:

题目理解比较容易,无非就是先搬大件,再搬小件

首先就把货物的单元排序,然后....额,发现货物的数量没有办法一起带进来

如果考虑一维数组来存放所有信息,比如[0] - 数量,[1] - 单元,好像也没有办法进行排序

如果能解决同时存放两个信息,这个题就很好解了

之前在排序算法当中,有一种排序无需交换数组,排序算法之计数排序

解决的问题是统计每种数的数量,我们可以利用这一个特性来解决

比如:

boxTypes = [[2,5], [5,10], [4,7], [3,9]]

数量单元
25
510
47
39

如果用统计数组来存放的话

       

Idx0123456789
Val2435

优化数组空间的话,数组长度为 max - min +1 =10-5+1=6

offset = 0 - min = -5

Idx012345
Val2435

还原的话就是 Idx = Idx - offset

我们用 Idx 表示单元,Val 表示数量 

Idx-0ffset5678910
Val2435

另外,在构建计数数组时,可能会遇到相同单元的元素,那么需要累加数量

剩下的就是考虑,箱子与货车空间的情况,有可能装完,有可能装不完等等

方法一、计数数组

int maximumUnits(int** boxTypes, int boxTypesSize, int* boxTypesColSize, int truckSize){
    int row = boxTypesSize;
    int col = boxTypesColSize[0];
    int maxNum = 0;
    int i;

    if(row == 1)
        return boxTypes[0][0] * boxTypes[0][1];

    /* using counting array to hold all infos */
    int min = boxTypes[0][1];
    int max = boxTypes[row-1][1];

    // find min & max
    for(i=0; i<row; i++)
    {
        //printf("boxTypes[%d][1]=%d\n", i, boxTypes[i][1]);
        if(min > boxTypes[i][1])
            min = boxTypes[i][1];
        
        if(max < boxTypes[i][1])
            max = boxTypes[i][1];
    }
    //printf("min=%d, max=%d\n", min, max);

    int offset = 0 - min;
    int countLen = max - min + 1;
    int *countArray = (int *)malloc(sizeof(int) * countLen);

    // init countArray to zero
    for(i=0; i<countLen; i++)
        countArray[i] = 0;

    // fill countArray
    for(i=0; i<row; i++)
    {
        // printf("countArray[%d]=%d, boxType[%d][0]=%d\n", 
        //     boxTypes[i][1] + offset, countArray[ boxTypes[i][1] + offset], i, boxTypes[i][0]);

            countArray[ boxTypes[i][1] + offset] += boxTypes[i][0]; 
    }

    // debug
    // for(i=0; i<countLen; i++)
    //     printf("countArray[%d]=[%d]\n", i, countArray[i]);

    // pick box
    i = 0;
    while(truckSize)
    {
        // printf("truckSize=%d, countArray[%d]=%d, unit=%d\n", 
        //     truckSize, countLen-1-i, countArray[countLen-1-i], countLen-1-i - offset);

        if(truckSize >= countArray[countLen-1-i])   // truck can hold such box
        {
            maxNum += countArray[countLen-1-i] * (countLen-1-i - offset);
            truckSize -= countArray[countLen-1-i];
        }
        else    // truck can't hold such box
        {
            maxNum += truckSize * (countLen-1-i - offset);
            truckSize = 0;
        }

        if(countLen-1-i == 0)   // all box in, truck not filled
            break;

        i++;
    }

    free(countArray);
    return maxNum;
}

查看更多刷题笔记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值