File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
practice/problems/capacity_to_ship_packages_within_d_days Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments