Skip to content

Commit

Permalink
Time: 3 ms (66.30%), Space: 7.3 MB (62.47%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Feb 27, 2023
1 parent 1e69d24 commit 17e570a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 0410-split-array-largest-sum/0410-split-array-largest-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution {
public:
int isChk(vector<int>& nums, int k,int mid)
{
int sum=0;
int cnt=1;
for(int i=0;i<nums.size();i++)
{
sum+=nums[i];
if(nums[i] > mid) {
return false;
}
if(sum>mid)
{
cnt++;
sum=nums[i];
}
if(cnt > k) return false;
}
return true;
}
int splitArray(vector<int>& nums, int k) {
int s=*max_element(nums.begin(),nums.end());
int e=accumulate(nums.begin(),nums.end(),0);
int ans=0;
while(s<=e)
{
int mid=(s+e)/2;
if(s==e)return mid;
if(isChk(nums,k,mid))
{
ans=mid;
e=mid;

}
else
s=mid+1;
}
return ans;

}
};

0 comments on commit 17e570a

Please sign in to comment.