首页 > 其他分享 >leetcode 1687. 从仓库到码头运输箱子 动态规划 + 单调队列

leetcode 1687. 从仓库到码头运输箱子 动态规划 + 单调队列

时间:2022-12-05 23:55:34浏览次数:36  
标签:箱子 卡车 队列 行程 仓库 boxes 码头 1687 leetcode

你有一辆货运卡车,你需要用这一辆车把一些箱子从仓库运送到码头。这辆卡车每次运输有 箱子数目的限制 和 总重量的限制 。

给你一个箱子数组 boxes 和三个整数 portsCount, maxBoxes 和 maxWeight ,其中 boxes[i] = [ports​​i​, weighti] 。

ports​​i 表示第 i 个箱子需要送达的码头, weightsi 是第 i 个箱子的重量。
portsCount 是码头的数目。
maxBoxes 和 maxWeight 分别是卡车每趟运输箱子数目和重量的限制。
箱子需要按照 数组顺序 运输,同时每次运输需要遵循以下步骤:

卡车从 boxes 队列中按顺序取出若干个箱子,但不能违反 maxBoxes 和 maxWeight 限制。
对于在卡车上的箱子,我们需要 按顺序 处理它们,卡车会通过 一趟行程 将最前面的箱子送到目的地码头并卸货。如果卡车已经在对应的码头,那么不需要 额外行程 ,箱子也会立马被卸货。
卡车上所有箱子都被卸货后,卡车需要 一趟行程 回到仓库,从箱子队列里再取出一些箱子。
卡车在将所有箱子运输并卸货后,最后必须回到仓库。

请你返回将所有箱子送到相应码头的 最少行程 次数。

 

示例 1:

输入:boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3
输出:4
解释:最优策略如下:
- 卡车将所有箱子装上车,到达码头 1 ,然后去码头 2 ,然后再回到码头 1 ,最后回到仓库,总共需要 4 趟行程。
所以总行程数为 4 。
注意到第一个和第三个箱子不能同时被卸货,因为箱子需要按顺序处理(也就是第二个箱子需要先被送到码头 2 ,然后才能处理第三个箱子)。
示例 2:

输入:boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6
输出:6
解释:最优策略如下:
- 卡车首先运输第一个箱子,到达码头 1 ,然后回到仓库,总共 2 趟行程。
- 卡车运输第二、第三、第四个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。
- 卡车运输第五个箱子,到达码头 3 ,回到仓库,总共 2 趟行程。
总行程数为 2 + 2 + 2 = 6 。
示例 3:

输入:boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7
输出:6
解释:最优策略如下:
- 卡车运输第一和第二个箱子,到达码头 1 ,然后回到仓库,总共 2 趟行程。
- 卡车运输第三和第四个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。
- 卡车运输第五和第六个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。
总行程数为 2 + 2 + 2 = 6 。
示例 4:

输入:boxes = [[2,4],[2,5],[3,1],[3,2],[3,7],[3,1],[4,4],[1,3],[5,2]], portsCount = 5, maxBoxes = 5, maxWeight = 7
输出:14
解释:最优策略如下:
- 卡车运输第一个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。
- 卡车运输第二个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。
- 卡车运输第三和第四个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。
- 卡车运输第五个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。
- 卡车运输第六和第七个箱子,到达码头 3 ,然后去码头 4 ,然后回到仓库,总共 3 趟行程。
- 卡车运输第八和第九个箱子,到达码头 1 ,然后去码头 5 ,然后回到仓库,总共 3 趟行程。
总行程数为 2 + 2 + 2 + 2 + 3 + 3 = 14 。
 

提示:

1 <= boxes.length <= 105
1 <= portsCount, maxBoxes, maxWeight <= 105
1 <= ports​​i <= portsCount
1 <= weightsi <= maxWeight
通过次数10,233提交次数17,083

方法一:动态规划 + 单调队列

我们定义 f[i]f[i] 表示把前 ii 个箱子从仓库运送到相应码头的最少行程数,那么答案就是 f[n]f[n]。

箱子需要按数组顺序运输,每一次运输,卡车会按顺序取出连续的几个箱子,然后依次送往对应的码头,全部送达之后,又回到了仓库。

因此,我们可以枚举上一次运输的最后一个箱子的编号 jj,那么 f[i]f[i] 就可以从 f[j]f[j] 转移而来,转移的时候,我们需要考虑以下几个问题:

从 f[j]f[j] 转移过来的时候,卡车上的箱子数量不能超过 maxBoxesmaxBoxes
从 f[j]f[j] 转移过来的时候,卡车上的箱子总重量不能超过 maxWeightmaxWeight

简单举个例子,假设我们取出了 1, 2, 31,2,3 这三个箱子,需要送往 4, 4, 54,4,5 这三个码头,那么我们首先要从仓库到 44 号码头,然后再从 44 号码头到 55 号码头,最后再从 55 号码头回到仓库。可以发现,从仓库到码头,以及从码头到仓库,需要花费 22 趟行程,而从码头到码头的行程数,取决于相邻两个码头是否相同,如果不相同,那么行程数会增加 11,否则不变。因此,我们可以通过前缀和,计算出码头之间的行程数,再加上首尾两趟行程,就能把 [j+1,..i][j+1,..i] 这些箱子送往对应的码头所需要的行程数计算出来。

代码实现如下:
# 33/39 个通过测试用例,超出时间限制
class Solution:
    def boxDelivering(self, boxes: List[List[int]], portsCount: int, maxBoxes: int, maxWeight: int) -> int:
        n = len(boxes)
        ws = list(accumulate((box[1] for box in boxes), initial=0))
        c = [int(a != b) for a, b in pairwise(box[0] for box in boxes)]
        cs = list(accumulate(c, initial=0))
        f = [inf] * (n + 1)
        f[0] = 0
        for i in range(1, n + 1):
            for j in range(max(0, i - maxBoxes), i):
                if ws[i] - ws[j] <= maxWeight:
                    f[i] = min(f[i], f[j] + cs[i - 1] - cs[j] + 2)
        return f[n]
class Solution:
    def boxDelivering(self, boxes: List[List[int]], portsCount: int, maxBoxes: int, maxWeight: int) -> int:
        n = len(boxes)
        ws = list(accumulate((box[1] for box in boxes), initial=0))
        c = [int(a != b) for a, b in pairwise(box[0] for box in boxes)]
        cs = list(accumulate(c, initial=0))
        f = [0] * (n + 1)
        q = deque([0])
        for i in range(1, n + 1):
            while q and (i - q[0] > maxBoxes or ws[i] - ws[q[0]] > maxWeight):
                q.popleft()
            if q:
                f[i] = cs[i - 1] + f[q[0]] - cs[q[0]] + 2
            if i < n:
                while q and f[q[-1]] - cs[q[-1]] >= f[i] - cs[i]:
                    q.pop()
                q.append(i)
        return f[n]
时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 是题目中箱子的数量

 

标签:箱子,卡车,队列,行程,仓库,boxes,码头,1687,leetcode
From: https://www.cnblogs.com/xianbin7/p/16953955.html

相关文章

  • LeetCode刷题记录.Day32
    翻转二叉树递归法classSolution{public:TreeNode*invertTree(TreeNode*root){if(root==nullptr)returnroot;swap(root->left,root->......
  • LeetCode397. Integer Replacement
    题意一个数n,若为偶数,则除2,若为奇数,则加减1;求其最终为1,需要几步方法位运算代码classSolution{public:intintegerReplacement(intn){i......
  • [Leetcode Weekly Contest]322
    链接:LeetCode[Leetcode]2490.回环句句子是由单个空格分隔的一组单词,且不含前导或尾随空格。例如,"HelloWorld"、"HELLO"、"helloworldhelloworld"都是符合要求的......
  • LeetCode: 290. Word Pattern
    LeetCode:290.WordPattern题目描述Givena​​pattern​​​andastring​​str​​​,findif​​str​​​followsthesame​​pattern​​.Herefollowmea......
  • LeetCode: 310. Minimum Height Trees
    LeetCode:310.MinimumHeightTrees题目描述Foranundirectedgraphwithtreecharacteristics,wecanchooseanynodeastheroot.Theresultgraphisthenaro......
  • LeetCode: 312. Burst Balloons
    LeetCode:312.BurstBalloons题目描述Givennballoons,indexedfrom0ton-1.Eachballoonispaintedwithanumberonitrepresentedbyarraynums.Youareas......
  • LeetCode: 328. Odd Even Linked List
    LeetCode:328.OddEvenLinkedList题目描述Givenasinglylinkedlist,groupalloddnodestogetherfollowedbytheevennodes.Pleasenoteherewearetalking......
  • LeetCode: 313. Super Ugly Number
    LeetCode:313.SuperUglyNumberWriteaprogramtofindthenthsuperuglynumber.Superuglynumbersarepositivenumberswhoseallprimefactorsareinthegi......
  • LeetCode: 315. Count of Smaller Numbers After Self
    LeetCode:315.CountofSmallerNumbersAfterSelf题目描述Youaregivenanintegerarraynumsandyouhavetoreturnanewcountsarray.Thecountsarrayhast......
  • LeetCode: 322. Coin Change
    LeetCode:322.CoinChange题目描述Youaregivencoinsofdifferentdenominationsandatotalamountofmoneyamount.Writeafunctiontocomputethefewestnum......