LC 1011. 在 D 天内送达包裹的能力

题目描述

这是 LeetCode 上的 1011. 在 D 天内送达包裹的能力 ,难度为 中等

传送带上的包裹必须在 D 天内从一个港口运送到另一个港口。

传送带上的第 i 个包裹的重量为 $weights[i]$。

每一天,我们都会按给出重量的顺序往传送带上装载包裹。

我们装载的重量不会超过船的最大运载重量。

返回能在 D 天内将传送带上的所有包裹送达的船的最低运载能力。

示例 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
输入:weights = [1,2,3,4,5,6,7,8,9,10], D = 5

输出:15

解释:
船舶最低载重 15 就能够在 5 天内送达所有包裹,如下所示:
1 天:1, 2, 3, 4, 5
2 天:6, 7
3 天:8
4 天:9
5 天:10

请注意,货物必须按照给定的顺序装运,因此使用载重能力为 14 的船舶并将包装分成 (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) 是不允许的。

示例 2:
1
2
3
4
5
6
7
8
9
输入:weights = [3,2,2,4,1,4], D = 3

输出:6

解释:
船舶最低载重 6 就能够在 3 天内送达所有包裹,如下所示:
1 天:3, 2
2 天:2, 4
3 天:1, 4

示例 3:
1
2
3
4
5
6
7
8
9
输入:weights = [1,2,3,1,1], D = 4

输出:3

解释:
1 天:1
2 天:2
3 天:3
4 天:1, 1

提示:

  • $1 <= D <= weights.length <= 5 \times 10^4$
  • $1 <= weights[i] <= 500$

二分解法(精确边界)

假定「D 天内运送完所有包裹的最低运力」为 ans,那么在以 ans 为分割点的数轴上具有「二段性」:

  • 数值范围在 $(-\infty, ans)$ 的运力必然「不满足」 D 天内运送完所有包裹的要求
  • 数值范围在 $[ans, +\infty)$ 的运力必然「满足」 D天内运送完所有包裹的要求

我们可以通过「二分」来找到恰好满足 D天内运送完所有包裹的分割点 ans

接下来我们要确定二分的范围,由于不存在包裹拆分的情况,考虑如下两种边界情况:

  • 理论最低运力:只确保所有包裹能够被运送,自然也包括重量最大的包裹,此时理论最低运力为 maxmax 为数组 weights 中的最大值
  • 理论最高运力:使得所有包裹在最短时间(一天)内运送完成,此时理论最高运力为 sumsum 为数组 weights 的总和

由此,我们可以确定二分的范围为 $[max, sum]$。

Java 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int shipWithinDays(int[] weights, int days) {
int max = 0, sum = 0;
for (int w : weights) {
max = Math.max(max, w);
sum += w;
}
int l = max, r = sum;
while (l < r) {
int mid = l + r >> 1;
if (check(weights, mid, days)) r = mid;
else l = mid + 1;
}
return r;
}
boolean check(int[] weights, int t, int days) {
int n = weights.length, cnt = 1;
for (int i = 1, sum = weights[0]; i < n; sum = 0, cnt++) {
while (i < n && sum + weights[i] <= t) sum += weights[i++];
}
return cnt - 1 <= days;
}
}

C++ 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int shipWithinDays(vector<int>& weights, int days) {
int maxv = 0, sum = 0;
for (int w : weights) {
maxv = max(maxv, w);
sum += w;
}
int l = maxv, r = sum;
while (l < r) {
int mid = l + r >> 1;
if (check(weights, mid, days)) r = mid;
else l = mid + 1;
}
return r;
}
bool check(vector<int>& weights, int t, int days) {
int n = weights.size(), cnt = 1;
for (int i = 1, sum = weights[0]; i < n; sum = 0, cnt++) {
while (i < n && sum + weights[i] <= t) sum += weights[i++];
}
return cnt - 1 <= days;
}
};

Python 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
def check(weights: List[int], t: int, days: int) -> bool:
n, cnt = len(weights), 1
i, sumv = 1, weights[0]
while i < n:
while i < n and sumv + weights[i] <= t:
sumv += weights[i]
i += 1
cnt += 1
sumv = 0
return cnt - 1 <= days

maxv, sumv = max(weights), sum(weights)
l, r = maxv, sumv
while l < r:
mid = l + r >> 1
if check(weights, mid, days):
r = mid
else:
l = mid + 1
return r

TypeScript 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function shipWithinDays(weights: number[], days: number): number {
const check = function(weights: number[], t: number, days: number): boolean {
let n = weights.length, cnt = 1;
for (let i = 1, sum = weights[0]; i < n; sum = 0, cnt++) {
while (i < n && sum + weights[i] <= t) sum += weights[i++];
}
return cnt - 1 <= days;
}
let maxv = 0, sumv = 0;
for (const w of weights) {
maxv = Math.max(maxv, w);
sumv += w;
}
let l = maxv, r = sumv;
while (l < r) {
const mid = l + r >> 1;
if (check(weights, mid, days)) r = mid;
else l = mid + 1;
}
return r;
};

  • 时间复杂度:二分范围为 $[max, sum]$,check 函数的复杂度为 $O(n)$。整体复杂度为 $O(n\log({\sum_{i= 0}^{n - 1}ws[i]}))$
  • 空间复杂度:$O(1)$

二分解法(粗略边界)

当然,一个合格的「二分范围」只需要确保包含分割点 ans 即可。因此我们可以利用数据范围来确立粗略的二分范围(从而少写一些代码):

  • 利用运力必然是正整数,从而确定左边界为 $1$
  • 根据 $1 \leqslant D \leqslant weights.length \leqslant 50000$ 和 $1 \leqslant weights[i] \leqslant 500$,从而确定右边界为 $1e8$

PS. 由于二分查找具有折半效率,因此「确立粗略二分范围」不会比「通过循环取得精确二分范围」效率低。

Java 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int shipWithinDays(int[] weights, int days) {
int l = 1, r = (int)1e8;
while (l < r) {
int mid = l + r >> 1;
if (check(weights, mid, days)) r = mid;
else l = mid + 1;
}
return r;
}
boolean check(int[] weights, int t, int days) {
if (weights[0] > t) return false;
int n = weights.length, cnt = 1;
for (int i = 1, sum = weights[0]; i < n; sum = 0, cnt++) {
if (weights[i] > t) return false;
while (i < n && sum + weights[i] <= t) sum += weights[i++];
}
return cnt - 1 <= days;
}
}

C++ 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int shipWithinDays(vector<int>& weights, int days) {
int l = 1, r = 1e8;
while (l < r) {
int mid = l + r >> 1;
if (check(weights, mid, days)) r = mid;
else l = mid + 1;
}
return r;
}
bool check(vector<int>& weights, int t, int days) {
if (weights[0] > t) return false;
int n = weights.size(), cnt = 1;
for (int i = 1, sum = weights[0]; i < n; sum = 0, cnt++) {
if (weights[i] > t) return false;
while (i < n && sum + weights[i] <= t) sum += weights[i++];
}
return cnt - 1 <= days;
}
};

Python 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
def check(weights: List[int], t: int, days: int) -> bool:
if weights[0] > t: return False
n, cnt = len(weights), 1
i, sumv = 1, weights[0]
while i < n:
if weights[i] > t: return False
while i < n and sumv + weights[i] <= t:
sumv += weights[i]
i += 1
cnt += 1
sumv = 0
return cnt - 1 <= days

l, r = 1, 10**8
while l < r:
mid = l + r >> 1
if check(weights, mid, days):
r = mid
else:
l = mid + 1
return r

TypeScript 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function shipWithinDays(weights: number[], days: number): number {
const check = function(weights: number[], t: number, days: number): boolean {
if (weights[0] > t) return false;
let n = weights.length, cnt = 1;
for (let i = 1, sum = weights[0]; i < n; sum = 0, cnt++) {
if (weights[i] > t) return false;
while (i < n && sum + weights[i] <= t) sum += weights[i++];
}
return cnt - 1 <= days;
}
let l = 0, r = 1e8;
while (l < r) {
const mid = l + r >> 1;
if (check(weights, mid, days)) r = mid;
else l = mid + 1;
}
return r;
};

  • 时间复杂度:二分范围为 $[1, 1e8]$,check 函数的复杂度为 $O(n)$。整体复杂度为 $O(n\log{1e8})$
  • 空间复杂度:$O(1)$

最后

这是我们「刷穿 LeetCode」系列文章的第 No.1011 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!