Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Solution for Painter's Partition Problem #354

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions C++/paintersPartition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Given n boards of different lengths represented by an array arr of size n, and m painters,
you need to determine the minimum amount of time required to paint all the boards. Each painter paints a contiguous segment of boards.
Constraints:

A painter can only paint continuous segments of boards.
Each painter takes the same amount of time to paint a unit length of the board.
You have to minimize the maximum time taken by a painter to paint their assigned boards.
Input:
An integer n representing the number of boards.
An integer m representing the number of painters.
An array arr[] of size n, where each element arr[i] represents the length of the i-th board.
Output:
Return the minimum time required to paint all the boards such that no painter is overloaded with work.
*/

#include<iostream>
#include<vector>
#include <climits>
using namespace std;

bool isPossible(vector<int>&arr, int n, int m, int maxAllowedTime){
int painter = 1, time = 0;
for(int i=0; i<n; i++){
if(time+arr[i]<= maxAllowedTime){
time += arr[i];
} else{
painter++;
time = arr[i];

}
}
return painter<=m;
}
int minTimeToPaint(vector<int> &arr, int n, int m){
int sum =0, maxVal = INT_MIN;
for(int i=0; i<arr.size(); i++){
sum += arr[i];
maxVal = max(maxVal, arr[i]);
}

int st = maxVal, end = sum, ans = -1;
while(st<=end){
int mid = st + (end-st)/2;
if(isPossible(arr, n, m, mid)){
ans = mid;
end = mid-1;
}
else{
st = mid+1;
}

}
return ans;
}

int main(){
vector<int>arr = {40,30,10,20};
int n = 4, m =2;
cout<<minTimeToPaint(arr, n, m)<<endl;
return 0;
}
Loading