# 在D天Leetcode解决方案中运送包裹的能力

## 问题陈述

### 使用案列

`weights = [1,2,3,4,5,6,7,8,9,10], D = 5`
`15`

## 在D日内通过Leetcode解决方案运送包裹的方法

1. 船舶的承载能力应至少等于最大重量为max（weights）的包裹。 让我们将其命名为 Start 开始.
2. 我们可以将船的最大容量限制为所有小包的重量之和，即sum（weights）。 让我们将其命名为 结束.

### 可以在D天之内通过Leetcode解决方案运送包裹的能力的C ++代码

```#include <bits/stdc++.h>
using namespace std;
int shipWithinDays(vector<int>& weights, int D) {
int left = 0, right = 0;
for (int w: weights) {
left = max(left, w);
right += w;
}
while (left < right) {
int mid = (left + right) / 2, requirement = 1, tillnow = 0;
for (int w: weights) {
if (tillnow + w > mid) {
requirement += 1;
tillnow = 0;
}
tillnow += w;
}
if (requirement > D) left = mid + 1;
else right = mid;
}
return left;
}
int main()
{
vector<int> arr = {1,2,3,4,5,6,7,8,9,10};
int d=5;

cout<<shipWithinDays(arr,d)<<endl;
return 0;
}```
`15`

### 可以在D天之内通过Leetcode解决方案运送包裹的Java代码

```public class Tutorialcup {
public static int shipWithinDays(int[] weights, int D) {
int left = 0, right = 0;
for (int w: weights) {
left = Math.max(left, w);
right += w;
}
while (left < right) {
int mid = (left + right) / 2, requirement = 1, tillnow = 0;
for (int w: weights) {
if (tillnow + w > mid) {
requirement += 1;
tillnow = 0;
}
tillnow += w;
}
if (requirement > D) left = mid + 1;
else right = mid;
}
return left;
}
public static void main(String[] args) {
int [] arr = {1,2,3,4,5,6,7,8,9,10};
int d=5;
int ans= shipWithinDays(arr,d);
System.out.println(ans);
}
}```
`15`