File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
410-split-array-largest-sum Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments