Skip to content

Commit 4cef6e5

Browse files
committed
Time: 0 ms (100.00%) | Memory: 9.4 MB (21.70%) - LeetSync
1 parent 878d418 commit 4cef6e5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int canAllocate(vector<int>& arr, int pages){
4+
int noOfStd = 1;
5+
long long pagesStudent = 0;
6+
for(int i = 0; i < arr.size(); i++){
7+
if(pagesStudent + arr[i] <= pages){
8+
pagesStudent += arr[i];
9+
}
10+
else{
11+
noOfStd += 1;
12+
pagesStudent = arr[i];
13+
}
14+
}
15+
return noOfStd;
16+
}
17+
18+
int splitArray(vector<int>& arr, int k) {
19+
if(k > arr.size()) return -1;
20+
int low = *max_element(arr.begin(), arr.end());
21+
int high = accumulate(arr.begin(), arr.end(), 0);
22+
while(low <= high){
23+
int mid = low + (high - low) / 2;
24+
int students = canAllocate(arr, mid);
25+
if(students > k) low = mid + 1;
26+
else high = mid - 1;
27+
}
28+
return low;
29+
}
30+
};

0 commit comments

Comments
 (0)