Skip to content

Commit b8eeec7

Browse files
authored
feat: add java solution to lc problem: 1011. Capacity To Ship Packages Within D Days (#547)
1 parent 9e0f78f commit b8eeec7

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,38 @@
8080
<!-- 这里可写当前语言的特殊实现逻辑 -->
8181

8282
```java
83-
83+
class Solution {
84+
public int shipWithinDays(int[] weights, int days) {
85+
int left = 1, right = Integer.MAX_VALUE;
86+
while (left < right) {
87+
int mid = (left + right) >> 1;
88+
if (canCarry(weights, days, mid)) {
89+
right = mid;
90+
} else {
91+
left = mid + 1;
92+
}
93+
}
94+
return left;
95+
}
96+
97+
98+
public boolean canCarry(int[] weights, int days, int carry) {
99+
int useDay = 1;
100+
int curCarry = 0;
101+
for (int weight : weights) {
102+
if (weight > carry) {
103+
return false;
104+
}
105+
if ((carry - curCarry) >= weight) {
106+
curCarry += weight;
107+
} else {
108+
curCarry = weight;
109+
useDay++;
110+
}
111+
}
112+
return useDay <= days;
113+
}
114+
}
84115
```
85116

86117
### **...**

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,38 @@ Note that the cargo must be shipped in the order given, so using a ship of capac
7171
### **Java**
7272

7373
```java
74-
74+
class Solution {
75+
public int shipWithinDays(int[] weights, int days) {
76+
int left = 1, right = Integer.MAX_VALUE;
77+
while (left < right) {
78+
int mid = (left + right) >> 1;
79+
if (canCarry(weights, days, mid)) {
80+
right = mid;
81+
} else {
82+
left = mid + 1;
83+
}
84+
}
85+
return left;
86+
}
87+
88+
89+
public boolean canCarry(int[] weights, int days, int carry) {
90+
int useDay = 1;
91+
int curCarry = 0;
92+
for (int weight : weights) {
93+
if (weight > carry) {
94+
return false;
95+
}
96+
if ((carry - curCarry) >= weight) {
97+
curCarry += weight;
98+
} else {
99+
curCarry = weight;
100+
useDay++;
101+
}
102+
}
103+
return useDay <= days;
104+
}
105+
}
75106
```
76107

77108
### **...**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int shipWithinDays(int[] weights, int days) {
3+
int left = 1, right = Integer.MAX_VALUE;
4+
while (left < right) {
5+
int mid = (left + right) >> 1;
6+
if (canCarry(weights, days, mid)) {
7+
right = mid;
8+
} else {
9+
left = mid + 1;
10+
}
11+
}
12+
return left;
13+
}
14+
15+
16+
public boolean canCarry(int[] weights, int days, int carry) {
17+
int useDay = 1;
18+
int curCarry = 0;
19+
for (int weight : weights) {
20+
if (weight > carry) {
21+
return false;
22+
}
23+
if ((carry - curCarry) >= weight) {
24+
curCarry += weight;
25+
} else {
26+
curCarry = weight;
27+
useDay++;
28+
}
29+
}
30+
return useDay <= days;
31+
}
32+
}

0 commit comments

Comments
 (0)