Title / Description
Code class Solution { private: int ok(vector<int> arr,int pages) { int cnt=1,curr=0; for(int i:arr) { if(curr+i<=pages) { curr+=i; } else { cnt++; curr=i; } } return cnt; } public: int findPages(vector<int> &arr, int k) { // code here int n=arr.size(); if(n<k) return -1; if(k==1) return accumulate(arr.begin(),arr.end(),0); int l=*max_element(arr.begin(),arr.end()); int h=accumulate(arr.begin(),arr.end(),0); int ans=INT_MAX; while(l<=h) { int m=l+(h-l)/2; int s=ok(arr,m); if(s<=k) { ans=min(ans,m); h=m-1; } else { l=m+1; } } return ans==INT_MAX?-1:ans; } };
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code