-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
919c3f1
commit 78733a1
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
The Painter's Partition Problem-II - GFG/the-painters-partition-problem-ii.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//{ Driver Code Starts | ||
// driver code | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
// } Driver Code Ends | ||
//User function template for C++ | ||
|
||
class Solution | ||
{ | ||
public: | ||
bool solve(int mid, int arr[], int n, int k){ | ||
int painter=1; | ||
int curBooks=0; | ||
for(int i=0;i<n;i++){ | ||
if(curBooks+arr[i]<=mid){ | ||
curBooks+=arr[i]; | ||
} | ||
else{ | ||
painter++; | ||
curBooks=arr[i]; | ||
} | ||
} | ||
if(painter<=k){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
long long minTime(int arr[], int n, int k) | ||
{ | ||
// code here | ||
// return minimum time | ||
long long sum=0; | ||
int maxi=INT_MIN; | ||
for(int i=0;i<n;i++){ | ||
sum+=arr[i]; | ||
maxi=max(maxi,arr[i]); | ||
} | ||
|
||
long long s=maxi; | ||
long long e=sum; | ||
|
||
long long ans=INT_MAX; | ||
while(s<=e){ | ||
int mid=s+(e-s)/2; | ||
if(solve(mid,arr,n,k)){ | ||
ans=mid; | ||
e=mid-1; | ||
} | ||
else{ | ||
s=mid+1; | ||
} | ||
} | ||
return ans; | ||
} | ||
}; | ||
|
||
//{ Driver Code Starts. | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin>>t; | ||
while(t--) | ||
{ | ||
int k,n; | ||
cin>>k>>n; | ||
|
||
int arr[n]; | ||
for(int i=0;i<n;i++) | ||
cin>>arr[i]; | ||
Solution obj; | ||
cout << obj.minTime(arr, n, k) << endl; | ||
} | ||
return 0; | ||
} | ||
// } Driver Code Ends |