8000 Sync LeetCode submission - Capacity To Ship Packages Within D Days (j… · thatbeautifuldream/leetcode-sync@346b77e · GitHub
[go: up one dir, main page]

Skip to content

Commit 346b77e

Browse files
Sync LeetCode submission - Capacity To Ship Packages Within D Days (java)
1 parent 3cd1aeb commit 346b77e

File tree

1 file changed

+39
-0
lines changed
  • practice/problems/capacity_to_ship_packages_within_d_days

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public int shipWithinDays(int[] wt, int days) {
3+
int max = 0;
4+
int sum = 0;
5+
for(int val : wt) {
6+
sum += val;
7+
max = Math.max(max, val);
8+
}
9+
if(wt.length == days) {
10+
return max;
11+
}
12+
int low = max;
13+
int high = sum;
14+
int ans = 0;
15+
while(low <= high) {
16+
int mid = low + (high - low)/2;
17+
if(isPossible(wt, mid, days) == true) {
18+
ans = mid;
19+
high = mid - 1;
20+
}
21+
else {
22+
low = mid + 1;
23+
}
24+
}
25+
return ans;
26+
}
27+
public static boolean isPossible(int[] wt, int mid, int days) {
28+
int d = 1;
29+
int sum = 0;
30+
for(int i = 0; i<wt.length; i++) {
31+
sum += wt[i];
32+
if(sum > mid){
33+
d++;
34+
sum = wt[i];
35+
}
36+
}
37+
return d<=days;
38+
}
39+
}

0 commit comments

Comments
 (0)
0