-
Notifications
You must be signed in to change notification settings - Fork 30
/
Capacity To Ship Packages Within D Days.cpp
79 lines (61 loc) · 1.81 KB
/
Capacity To Ship Packages Within D Days.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Solution by Rahul Surana
***********************************************************
Given an array arr[] of N weights.
Find the least weight capacity of a boat to ship all weights within D days.
The ith weight has a weight of arr[i]. Each day, we load the boat with weights given by arr[i].
We may not load more weight than the maximum weight capacity of the ship.
***********************************************************
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution {
public:
int leastWeightCapacity(int arr[], int N, int D) {
int s = 0,ma=0;
for(int i = 0; i < N; i++){
s += arr[i];
ma = max(arr[i],ma);
}
int l = 0 , r = 500000001;
int ans = 0;
while(l<=r){
int m = l+((r-l)/2);
int z = 0,a = 1;
for(int i = 0; i < N; i++){
z+=arr[i];
if(z > m) {a++;
z = arr[i];}
}
if(a<=D) { r = m-1; ans = m; }
else { l = m+1; }
}
// for(int i = 0; i < N; i++){
// if(arr[i] < x && i < N-1){
// ans = max(ans,arr[i] + arr[i+1]);
// i++;
// }
// }
// cout << ans <<" ";
return (ans<ma)? ma: ans;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N,D;
cin>>N;
int arr[N];
for(int i=0; i<N; i++)
cin>>arr[i];
cin>>D;
Solution ob;
cout << ob.leastWeightCapacity(arr,N,D) << endl;
}
return 0;
} // } Driver Code Ends