Medium
题目描述
传送带上的包裹必须在 days 天内从一个港口运送到另一个港口。
传送带上第 i 个包裹的重量为 weights[i]。每一天,我们都会按给出重量的顺序往船上装载包裹。我们装载的重量不会超过船的最大运载重量。
返回能在 days 天内将传送带上的所有包裹送达的船的最低运载能力。
示例 1:
输入:weights = [1,2,3,4,5,6,7,8,9,10], days = 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:
输入:weights = [3,2,2,4,1,4], days = 3
输出:6
解释:
船舶最低载重 6 就能够在 3 天内送达所有包裹,如下所示:
第 1 天:3, 2
第 2 天:2, 4
第 3 天:1, 4
示例 3:
输入:weights = [1,2,3,1,1], days = 4
输出:3
解释:
第 1 天:1
第 2 天:2
第 3 天:3
第 4 天:1, 1
提示:
1 <= days <= weights.length <= 5 * 10^41 <= weights[i] <= 500
解题思路
这道题是一个经典的二分搜索问题。我们需要找到最小的船只载重能力,使得能在指定天数内运完所有包裹。
思路分析:
确定搜索范围:
- 最小载重:必须至少能装下最重的包裹,即
max(weights) - 最大载重:一次性装完所有包裹,即
sum(weights)
- 最小载重:必须至少能装下最重的包裹,即
二分搜索策略:
- 对载重能力进行二分搜索
- 对于每个中间值,判断是否能在规定天数内完成运输
- 如果可以,尝试更小的载重;如果不行,增加载重
验证函数设计:
- 模拟装船过程:按顺序装包裹,当前船装不下就换下一天
- 统计总天数,判断是否不超过限制
时间复杂度优化:
- 二分搜索的时间复杂度为 O(log(sum-max))
- 每次验证需要 O(n) 时间遍历所有包裹
- 总复杂度:O(n * log(sum-max))
这种方法的核心思想是"最小化最大值"问题的标准解法。通过二分搜索在答案区间内找到满足条件的最小值。
代码实现
class Solution {
public:
int shipWithinDays(vector<int>& weights, int days) {
int left = *max_element(weights.begin(), weights.end());
int right = accumulate(weights.begin(), weights.end(), 0);
while (left < right) {
int mid = left + (right - left) / 2;
if (canShip(weights, days, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
private:
bool canShip(const vector<int>& weights, int days, int capacity) {
int currentWeight = 0;
int daysNeeded = 1;
for (int weight : weights) {
if (currentWeight + weight > capacity) {
daysNeeded++;
currentWeight = weight;
} else {
currentWeight += weight;
}
}
return daysNeeded <= days;
}
};
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
left, right = max(weights), sum(weights)
while left < right:
mid = (left + right) // 2
if self.canShip(weights, days, mid):
right = mid
else:
left = mid + 1
return left
def canShip(self, weights: List[int], days: int, capacity: int) -> bool:
current_weight = 0
days_needed = 1
for weight in weights:
if current_weight + weight > capacity:
days_needed += 1
current_weight = weight
else:
current_weight += weight
return days_needed <= days
public class Solution {
public int ShipWithinDays(int[] weights, int days) {
int left = weights.Max();
int right = weights.Sum();
while (left < right) {
int mid = left + (right - left) / 2;
if (CanShip(weights, days, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
private bool CanShip(int[] weights, int days, int capacity) {
int currentWeight = 0;
int daysNeeded = 1;
foreach (int weight in weights) {
if (currentWeight + weight > capacity) {
daysNeeded++;
currentWeight = weight;
} else {
currentWeight += weight;
}
}
return daysNeeded <= days;
}
}
var shipWithinDays = function(weights, days) {
let left = Math.max(...weights);
let right = weights.reduce((sum, weight) => sum + weight, 0);
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (canShip(weights, days, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
function canShip(weights, days, capacity) {
let currentWeight = 0;
let daysNeeded = 1;
for (let weight of weights) {
if (currentWeight + weight > capacity) {
daysNeeded++;
currentWeight = weight;
} else {
currentWeight += weight;
}
}
return daysNeeded <= days;
}
复杂度分析
| 复杂度类型 | 值 | 说明 |
|---|---|---|
| 时间复杂度 | O(n × log(sum - max)) | n 为包裹数量,二分搜索 log(sum-max) 次,每次验证需要 O(n) |
| 空间复杂度 | O(1) | 只使用常数额外空间 |
相关题目
. Split Array Largest Sum (Hard)
. Divide Chocolate (Hard)
. Cutting Ribbons (Medium)
. Minimized Maximum of Products Distributed to Any Store (Medium)