Skip to content

Commit 9d84e91

Browse files
committed
feat: add solutions to lc problem: No.1011. Capacity To Ship Packages
Within D Days
1 parent 2b6dadc commit 9d84e91

File tree

5 files changed

+296
-5
lines changed

5 files changed

+296
-5
lines changed

solution/1000-1099/1011.Capacity To Ship Packages Within D Days/README.md

+104-3
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,43 @@
6060
<li><code>1 &lt;= weights[i] &lt;= 500</code></li>
6161
</ol>
6262

63-
6463
## 解法
6564

6665
<!-- 这里可写通用的实现逻辑 -->
6766

67+
二分查找。
68+
69+
二分枚举运载能力 capacity,找到能在 D 天内送达的最小运载。
70+
6871
<!-- tabs:start -->
6972

7073
### **Python3**
7174

7275
<!-- 这里可写当前语言的特殊实现逻辑 -->
7376

7477
```python
75-
78+
class Solution:
79+
def shipWithinDays(self, weights: List[int], D: int) -> int:
80+
def check(capacity):
81+
cnt, t = 1, 0
82+
for w in weights:
83+
if w > capacity:
84+
return False
85+
if t + w <= capacity:
86+
t += w
87+
else:
88+
cnt += 1
89+
t = w
90+
return cnt <= D
91+
92+
left, right = 1, 25000000
93+
while left < right:
94+
mid = (left + right) >> 1
95+
if check(mid):
96+
right = mid
97+
else:
98+
left = mid + 1
99+
return left
76100
```
77101

78102
### **Java**
@@ -114,11 +138,88 @@ class Solution {
114138
}
115139
```
116140

141+
### **C++**
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
int shipWithinDays(vector<int> &weights, int days) {
147+
int left = 1, right = 25000000;
148+
while (left < right)
149+
{
150+
int mid = left + right >> 1;
151+
if (check(weights, days, mid))
152+
{
153+
right = mid;
154+
}
155+
else
156+
{
157+
left = mid + 1;
158+
}
159+
}
160+
return left;
161+
}
162+
163+
bool check(vector<int> &weights, int days, int capacity) {
164+
int cnt = 1, t = 0;
165+
for (auto w : weights)
166+
{
167+
if (w > capacity)
168+
{
169+
return false;
170+
}
171+
if (t + w <= capacity)
172+
{
173+
t += w;
174+
}
175+
else
176+
{
177+
++cnt;
178+
t = w;
179+
}
180+
}
181+
return cnt <= days;
182+
}
183+
};
184+
```
185+
186+
### **Go**
187+
188+
```go
189+
func shipWithinDays(weights []int, days int) int {
190+
left, right := 1, 25000000
191+
for left < right {
192+
mid := (left + right) >> 1
193+
if check(weights, days, mid) {
194+
right = mid
195+
} else {
196+
left = mid + 1
197+
}
198+
}
199+
return left
200+
}
201+
202+
func check(weights []int, days, capacity int) bool {
203+
cnt, t := 1, 0
204+
for _, w := range weights {
205+
if w > capacity {
206+
return false
207+
}
208+
if t+w <= capacity {
209+
t += w
210+
} else {
211+
cnt++
212+
t = w
213+
}
214+
}
215+
return cnt <= days
216+
}
217+
```
218+
117219
### **...**
118220

119221
```
120222
121223
```
122224

123225
<!-- tabs:end -->
124-
<!-- tabs:end -->

solution/1000-1099/1011.Capacity To Ship Packages Within D Days/README_EN.md

+102-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,35 @@ Note that the cargo must be shipped in the order given, so using a ship of capac
6060

6161
## Solutions
6262

63+
Binary search.
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

6769
```python
68-
70+
class Solution:
71+
def shipWithinDays(self, weights: List[int], D: int) -> int:
72+
def check(capacity):
73+
cnt, t = 1, 0
74+
for w in weights:
75+
if w > capacity:
76+
return False
77+
if t + w <= capacity:
78+
t += w
79+
else:
80+
cnt += 1
81+
t = w
82+
return cnt <= D
83+
84+
left, right = 1, 25000000
85+
while left < right:
86+
mid = (left + right) >> 1
87+
if check(mid):
88+
right = mid
89+
else:
90+
left = mid + 1
91+
return left
6992
```
7093

7194
### **Java**
@@ -105,11 +128,88 @@ class Solution {
105128
}
106129
```
107130

131+
### **C++**
132+
133+
```cpp
134+
class Solution {
135+
public:
136+
int shipWithinDays(vector<int> &weights, int days) {
137+
int left = 1, right = 25000000;
138+
while (left < right)
139+
{
140+
int mid = left + right >> 1;
141+
if (check(weights, days, mid))
142+
{
143+
right = mid;
144+
}
145+
else
146+
{
147+
left = mid + 1;
148+
}
149+
}
150+
return left;
151+
}
152+
153+
bool check(vector<int> &weights, int days, int capacity) {
154+
int cnt = 1, t = 0;
155+
for (auto w : weights)
156+
{
157+
if (w > capacity)
158+
{
159+
return false;
160+
}
161+
if (t + w <= capacity)
162+
{
163+
t += w;
164+
}
165+
else
166+
{
167+
++cnt;
168+
t = w;
169+
}
170+
}
171+
return cnt <= days;
172+
}
173+
};
174+
```
175+
176+
### **Go**
177+
178+
```go
179+
func shipWithinDays(weights []int, days int) int {
180+
left, right := 1, 25000000
181+
for left < right {
182+
mid := (left + right) >> 1
183+
if check(weights, days, mid) {
184+
right = mid
185+
} else {
186+
left = mid + 1
187+
}
188+
}
189+
return left
190+
}
191+
192+
func check(weights []int, days, capacity int) bool {
193+
cnt, t := 1, 0
194+
for _, w := range weights {
195+
if w > capacity {
196+
return false
197+
}
198+
if t+w <= capacity {
199+
t += w
200+
} else {
201+
cnt++
202+
t = w
203+
}
204+
}
205+
return cnt <= days
206+
}
207+
```
208+
108209
### **...**
109210

110211
```
111212
112213
```
113214

114215
<!-- tabs:end -->
115-
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
int shipWithinDays(vector<int> &weights, int days) {
4+
int left = 1, right = 25000000;
5+
while (left < right)
6+
{
7+
int mid = left + right >> 1;
8+
if (check(weights, days, mid))
9+
{
10+
right = mid;
11+
}
12+
else
13+
{
14+
left = mid + 1;
15+
}
16+
}
17+
return left;
18+
}
19+
20+
bool check(vector<int> &weights, int days, int capacity) {
21+
int cnt = 1, t = 0;
22+
for (auto w : weights)
23+
{
24+
if (w > capacity)
25+
{
26+
return false;
27+
}
28+
if (t + w <= capacity)
29+
{
30+
t += w;
31+
}
32+
else
33+
{
34+
++cnt;
35+
t = w;
36+
}
37+
}
38+
return cnt <= days;
39+
}
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func shipWithinDays(weights []int, days int) int {
2+
left, right := 1, 25000000
3+
for left < right {
4+
mid := (left + right) >> 1
5+
if check(weights, days, mid) {
6+
right = mid
7+
} else {
8+
left = mid + 1
9+
}
10+
}
11+
return left
12+
}
13+
14+
func check(weights []int, days, capacity int) bool {
15+
cnt, t := 1, 0
16+
for _, w := range weights {
17+
if w > capacity {
18+
return false
19+
}
20+
if t+w <= capacity {
21+
t += w
22+
} else {
23+
cnt++
24+
t = w
25+
}
26+
}
27+
return cnt <= days
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def shipWithinDays(self, weights: List[int], D: int) -> int:
3+
def check(capacity):
4+
cnt, t = 1, 0
5+
for w in weights:
6+
if w > capacity:
7+
return False
8+
if t + w <= capacity:
9+
t += w
10+
else:
11+
cnt += 1
12+
t = w
13+
return cnt <= D
14+
15+
left, right = 1, 25000000
16+
while left < right:
17+
mid = (left + right) >> 1
18+
if check(mid):
19+
right = mid
20+
else:
21+
left = mid + 1
22+
return left

0 commit comments

Comments
 (0)