Skip to content
Open
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
93 changes: 93 additions & 0 deletions Find minimum time to finish all jobs with given constraints
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

class solution
{

static int getMax(int arr[], int n)
{
int result = arr[0];
for (int i=1; i<n; i++)
if (arr[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));
}
}