-
Notifications
You must be signed in to change notification settings - Fork 0
/
Capacity To Ship Packages Within D Days
54 lines (51 loc) · 1.13 KB
/
Capacity To Ship Packages Within D Days
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
bool ispossible(int mid , int arr[] , int d, int n)
{
bool check = true ;
int s = 0 ;
d-- ;
for(int i = 0 ; i < n ; i++ )
{
if(arr[i] > mid)
{
return false ;
}
else if(s + arr[i] <= mid)
{
s = s + arr[i] ;
}
else
{
s = arr[i] ;
d-- ;
}
}
return d >= 0 ;
}
int leastWeightCapacity(int arr[], int N, int D) {
// code here
int l = 1 ;
int w = 0 ;
for(int i = 0 ; i < N ; i++) {
w = w + arr[i] ;
}
// cout << w << endl ;
int r = w ;
int mid ;
while(l <= r)
{
mid =( l + r) / 2 ;
if(ispossible(mid ,arr , D , N))
{
// cout << mid << endl ;
r = mid - 1 ;
}
else
{
l = mid + 1 ;
}
}
return l ;
}
};