From a87f9d7cec7e1d121675f5470bac8c1556c0fa3e Mon Sep 17 00:00:00 2001 From: Pratap Pawar <87373605+pawarspeaks@users.noreply.github.com> Date: Tue, 31 Oct 2023 00:29:16 +0530 Subject: [PATCH] Create Find minimum time to finish all jobs with given constraints --- ... to finish all jobs with given constraints | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Find minimum time to finish all jobs with given constraints diff --git a/Find minimum time to finish all jobs with given constraints b/Find minimum time to finish all jobs with given constraints new file mode 100644 index 0000000..da68b89 --- /dev/null +++ b/Find minimum time to finish all jobs with given constraints @@ -0,0 +1,93 @@ + +class solution +{ + + static int getMax(int arr[], int n) + { + int result = arr[0]; + for (int i=1; i result) + result = arr[i]; + return result; + } + + + static boolean isPossible(int time, int K, + int job[], int n) + { + + int cnt = 1; + + int curr_time = 0; + + for (int i = 0; i < n;) + { + + if (curr_time + job[i] > time) { + curr_time = 0; + cnt++; + } + + // Else add time of job to current + // time and move to next job. + else + { + curr_time += job[i]; + i++; + } + } + + // Returns true if count + // is smaller than k + return (cnt <= K); + } + + // Returns minimum time required to + // finish given array of jobs + // k - number of assignees + // T - Time required by every assignee to finish 1 unit + // m - Number of jobs + static int findMinTime(int K, int T, int job[], int n) + { + // Set start and end for binary search + // end provides an upper limit on time + int end = 0, start = 0; + for (int i = 0; i < n; ++i) + end += job[i]; + + // Initialize answer + int ans = end; + + // Find the job that takes maximum time + int job_max = getMax(job, n); + + // Do binary search for + // minimum feasible time + while (start <= end) + { + int mid = (start + end) / 2; + + // If it is possible to finish jobs in mid time + if (mid >= job_max && isPossible(mid, K, job, n)) + { + ans = Math.min(ans, mid); + + end = mid - 1; + } + + else + start = mid + 1; + } + + return (ans * T); + } + + public static void main(String arg[]) + { + int job[] = {10, 7, 8, 12, 6, 8}; + int n = job.length; + int k = 4, T = 5; + System.out.println(findMinTime(k, T, job, n)); + } +} +