forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
76 lines (66 loc) · 3.18 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
This question is solved using greedy approach
- we need to select a customer which chef will serve first, here comes the greedy approach
we will select that customer whose pizza can be prepared in the least time
Ex - customers come at t1,t2,t3 their pizza can be prepared in 7,5,3
So chef will make pizza for 3rd customer because his pizza can prepared in 3 minutes\
- Now storing the data efficiently is big task
- We will store the order time of all customer in increasing order in array this can be done using array
- But we also need to store the currently available orders according to increasing cooking time, we will use minimum heap for this.
- Solution- We will loop through array
- Upon arriving at the ith order, we will insert it into the min heap if its order time is less than or equal to the current time.
- Otherwise, we will keep serving from the top of the heap until current time becomes larger than or equal to the order time of the ith order or the heap becomes empty.
- We will update the current time while inserting an order into the heap ( if needed ) and also while serving an order.
*/
import java.util.*;
public class solution {
public static void main(String[] args) {
//Inputs
Scanner in = new Scanner(System.in);
int numOfCustomers = in.nextInt(); //number of customer
Customer[] customers = new Customer[numOfCustomers]; //Customer Array
for (int i = 0; i < numOfCustomers; i++) {
int orderTime = in.nextInt();
int cookTime = in.nextInt();
customers[i] = new Customer(orderTime, cookTime);
}
in.close();
//Solution starts
Arrays.sort(customers, Comparator.comparingInt(o -> o.orderTime)); //Sorting the inputs on basis of order time
Queue<Customer> waitTime = new PriorityQueue<>(); //Min heap
long currentTime = 0L;
long totalWaitTime = 0L;
int index = 0;
while (!waitTime.isEmpty() || index < customers.length) {
while (index < customers.length && customers[index].orderTime <= currentTime) {
waitTime.add(customers[index]);
index++;
}
if (waitTime.isEmpty()) {
currentTime = customers[index].orderTime;
continue;
}
Customer served = waitTime.poll();
currentTime += served.cookTime;
totalWaitTime += currentTime - served.orderTime;
}
System.out.println(totalWaitTime / customers.length);
}
//Customer class for ease to solve the question
static class Customer implements Comparable<Customer> {
int orderTime;
int cookTime;
public Customer(int orderTime, int cookTime) {
this.orderTime = orderTime;
this.cookTime = cookTime;
}
//overriding the operator
@Override
public int compareTo(Customer o) {
if (this.cookTime > o.cookTime) {
return this.orderTime;
} else
return -1;
}
}
}