diff --git a/Lecture015 Binary Search Advanced Problems/PaintersPartition.cpp b/Lecture015 Binary Search Advanced Problems/PaintersPartition.cpp new file mode 100644 index 00000000..a6ebf362 --- /dev/null +++ b/Lecture015 Binary Search Advanced Problems/PaintersPartition.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +bool isPossible(vector &boards, int n, int k, int mid) { + int painterCount = 1; + int boardcount = 0; + + for(int i = 0; i < n; i++) + { + if(boardcount + boards[i] <= mid) + { + boardcount += boards[i]; + } + else + { + painterCount++; + if(painterCount > k || boards[i] > mid) + { + return false; + } + boardcount = boards[i]; //ensures that when a new painter is assigned board, they start with the count of the current board (boards[i]). + } + } + return true; +} + +int findLargestMinDistance(vector &boards, int k) +{ + + // Initialize the binary search range + int s = 0; + int boardsum = 0; + int n= boards.size(); + + for(int i = 0; i < n; i++) + { + boardsum += boards[i]; // Total boardsum of all board + } + + int e = boardsum; // End point: boardsum of all board + int ans = -1; + int mid = s + (e - s) / 2; + + // Binary search to find the minimum number of board + while(s <= e) + { + if(isPossible(boards, n, k, mid)) + { + ans = mid; + e = mid - 1; // Try for a smaller value + } + else + { + s = mid + 1; // Try for a larger value + } + mid = s + (e - s) / 2; + } + + return ans; +} +