Minimum time to fulfil all orders
Last Updated :
27 Sep, 2022
Geek is organizing a party at his house. For the party, he needs exactly N (N ? 103) donuts for the guests. Geek decides to order the donuts from a nearby restaurant, which has L chefs and each chef has a rank R. A chef with rank R can make 1 donut in the first R minutes, 1 more donut in next 2R minutes, 1 more donut in 3R minutes, and so on. The task is to find the minimum time in which all the orders will be fulfilled.
Examples:
Input: N = 10, L = 4, rank[] = {1, 2, 3, 4}
Output: 12
Explanation: Chef with rank 1, can make 4 donuts in time 1 + 2 + 3 + 4 = 10 mins
Chef with rank 2, can make 3 donuts in time 2 + 4 + 6 = 12 mins
Chef with rank 3, can make 2 donuts in time 3 + 6 = 9 mins
Chef with rank 4, can make 1 donuts in time = 4 minutes
Total donuts = 4 + 3 + 2 + 1 = 10 and
Maximum time taken by all = 12 minutes.
Input: N = 8, L = 8, rank[] = {1, 1, 1, 1, 1, 1, 1, 1}
Output: 1
Explanation: As all chefs are ranked 1, so each chef can make 1 donuts 1 min.
Total donuts = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 8 and min time = 1 minute
Naive approach: To solve the problem follow the below idea:
The idea is to linearly search in the time line, the answer will be the minimum time for which chefs can make N donuts
Follow the steps to solve the problem:
- We will iterate from 1 to 10000000 then check for every ith minute if chefs can make all of the N donuts
- For every ith minute, count the number of donuts each chef can make.
- Sum up all the donuts made by all the chef and check whether it is greater or equal to N
- We can easily calculate that the answer will lie between 10000000 for the given constraints.
Time Complexity: O(N * T) where T is the value of the time range.
Auxiliary Space: O(1)
Minimum Time to fulfill orders using Binary Search:
To solve the problem follow the below idea:
We can do a binary search on time line to find the minimum time taken by all to make all the donuts.
Follow the steps to solve the problem:
- As the answers lie between 1 and 10000000 so we will take 2 boundaries, left boundary is 1 and the right boundary is 10000000
- Now, we will check the midpoint of the current search space. mid = (left + right) / 2.
- Now let’s say X is the number of donuts all chefs can make at mid time.
- If X ? N then, mid may be the answer and we will reduce the search space by assigning the right boundary to mid – 1.
- When X < N, the answer will be greater than mid, so we can reduce the search space by assigning the left boundary to mid+1.
- The minimum time can be received at the end of the traversal.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int p, vector< int >& cook, int n, int mid)
{
int cnt = 0;
for ( int i = 0; i < n; i++) {
int c = 0;
int time = mid;
int perpratas = cook[i];
while ( time > 0) {
time = time - perpratas;
if ( time >= 0) {
c += 1;
perpratas += cook[i];
}
}
cnt += c;
if (cnt >= p)
return true ;
}
return false ;
}
int findMinTime( int n, vector< int >& arr, int l)
{
int s = 0, e = 10000007;
int mid, ans = -1;
while (s <= e) {
mid = (s + e) / 2;
if (isPossible(n, arr, l, mid)) {
ans = mid;
e = mid - 1;
}
else {
s = mid + 1;
}
}
return ans;
}
int main()
{
int N = 10, L = 4;
vector< int > rank = { 1, 2, 3, 4 };
cout << findMinTime(N, rank, L);
return 0;
}
|
Java
import java.io.*;
import java.util.Scanner;
class GFG {
public static boolean isPossible( int p, int [] cook, int n, int mid)
{
int cnt = 0 ;
for ( int i = 0 ; i < n; i++) {
int c = 0 ;
int time = mid;
int perpratas = cook[i];
while (time > 0 ) {
time = time - perpratas;
if (time >= 0 ) {
c += 1 ;
perpratas += cook[i];
}
}
cnt += c;
if (cnt >= p)
return true ;
}
return false ;
}
static int findMinTime( int n, int [] arr, int l)
{
int s = 0 , e = 10000007 ;
int mid, ans = - 1 ;
while (s <= e) {
mid = (s + e) / 2 ;
if (isPossible(n, arr, l, mid)) {
ans = mid;
e = mid - 1 ;
}
else {
s = mid + 1 ;
}
}
return ans;
}
public static void main(String[] args) {
int N = 10 , L = 4 ;
int [] rank = new int []{ 1 , 2 , 3 , 4 };
System.out.println(findMinTime(N, rank, L));
}
}
|
Python3
def isPossible(p, cook, n, mid):
cnt = 0
for i in range ( 0 , n):
c = 0
time = mid
perpratas = cook[i]
while (time > 0 ):
time = time - perpratas
if (time > = 0 ):
c + = 1
perpratas + = cook[i]
cnt + = c
if (cnt > = p):
return True
return False
def findMinTime(n, arr, l):
s, e = 0 , 10000007
mid, ans = 0 , - 1
while (s < = e):
mid = (s + e) / / 2
if (isPossible(n, arr, l, mid)):
ans = mid
e = mid - 1
else :
s = mid + 1
return ans
if __name__ = = "__main__" :
N = 10
L = 4
rank = [ 1 , 2 , 3 , 4 ]
print (findMinTime(N, rank, L))
|
C#
using System;
class GFG {
static bool isPossible( int p, int [] cook, int n, int mid)
{
int cnt = 0;
for ( int i = 0; i < n; i++) {
int c = 0;
int time = mid;
int perpratas = cook[i];
while (time > 0) {
time = time - perpratas;
if (time >= 0) {
c += 1;
perpratas += cook[i];
}
}
cnt += c;
if (cnt >= p)
return true ;
}
return false ;
}
static int findMinTime( int n, int [] arr, int l)
{
int s = 0, e = 10000007;
int mid, ans = -1;
while (s <= e) {
mid = (s + e) / 2;
if (isPossible(n, arr, l, mid)) {
ans = mid;
e = mid - 1;
}
else {
s = mid + 1;
}
}
return ans;
}
public static void Main( string [] args)
{
int N = 10, L = 4;
int [] rank = { 1, 2, 3, 4 };
Console.Write( findMinTime(N, rank, L));
}
}
|
Javascript
<script>
function isPossible(p, cook, n, mid) {
let cnt = 0;
for (let i = 0; i < n; i++) {
let c = 0;
let time = mid;
let perpratas = cook[i];
while (time > 0) {
time = time - perpratas;
if (time >= 0) {
c += 1;
perpratas += cook[i];
}
}
cnt += c;
if (cnt >= p)
return true ;
}
return false ;
}
function findMinTime(n, arr, l)
{
let s = 0, e = 10000007;
let mid, ans = -1;
while (s <= e) {
mid = Math.floor((s + e) / 2);
if (isPossible(n, arr, l, mid)) {
ans = mid;
e = mid - 1;
}
else {
s = mid + 1;
}
}
return ans;
}
let N = 10, L = 4;
let rank = [1, 2, 3, 4];
document.write(findMinTime(N, rank, L));
</script>
|
Time Complexity: O(N*logN).
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...