Skip to content

Instantly share code, notes, and snippets.

@viveknarang
Last active December 11, 2017 22:55
Show Gist options
  • Save viveknarang/87821aa895ce601d89ef924fb7ff4215 to your computer and use it in GitHub Desktop.
Save viveknarang/87821aa895ce601d89ef924fb7ff4215 to your computer and use it in GitHub Desktop.
public class Main {
public static void main(String[] args) {
int[][] arr = { { 0, 5 }, { 10, 25 }, { 30, 10 }, { 30, 5 }, { 299, 5 }, { 299, 1 } };
try {
new Engine(arr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.LinkedList;
import java.util.List;
public class Engine {
int[][] arr;
final int MAX_TIME = 300;
List<Customer> aList = new LinkedList<Customer>();
int maxQueueSize = 0;
int maxWaitTime = Integer.MIN_VALUE;
int minWaitTime = Integer.MAX_VALUE;
public Engine(int[][] arr) throws Exception {
if (arr == null || arr.length == 0) {
throw new Exception("Input data array cannot be null or empty!");
}
this.arr = arr;
this.aList = new LinkedList<Customer>();
this.execute();
this.print();
}
private void execute() {
int length = 0;
for (int i = 0; i < MAX_TIME; i++) {
System.out.println("At Time: " + i + " Length: " + length);
while (length < arr.length && i == arr[length][0]) {
Customer aCustomer = new Customer();
aCustomer.qETime = arr[length][0];
aCustomer.iWTime = arr[length][1];
aCustomer.eWTime = 0;
aCustomer.qWTime = 0;
if (aCustomer.qETime + aCustomer.iWTime < MAX_TIME) {
System.out.println("Adding .... " + aCustomer);
aList.add(aCustomer);
} else {
System.out.println("Discarding ... [" + arr[length][0] + "," + arr[length][1] + "]");
}
length++;
if (length >= arr.length) {
break;
}
}
int queueCount = 0;
boolean flag = false;
for (Customer c : aList) {
if (i >= c.qETime && c.iWTime > 0 && !flag) {
c.eWTime++;
c.iWTime--;
flag = true;
} else if (c.iWTime > 0) {
c.eWTime++;
c.qWTime++;
}
if (c.iWTime > 0) {
queueCount++;
}
}
if (queueCount > maxQueueSize) {
maxQueueSize = queueCount;
}
System.out.println(aList);
}
for (Customer c : aList) {
if (c.qWTime > maxWaitTime) {
maxWaitTime = c.qWTime;
}
if (c.qWTime < minWaitTime) {
minWaitTime = c.qWTime;
}
}
}
public void print() {
System.out.println("\n--------------------------- RESULT ----------------------------\n");
System.out.println("Maximum queue size: " + maxQueueSize);
System.out.println("Maximum wait time: " + maxWaitTime);
System.out.println("Maximum wait time: " + minWaitTime);
}
}
class Customer {
// Queue Entry Time ...
int qETime;
// Initial Wait Time ...
int iWTime;
// Effective Wait Time ...
int eWTime;
// Queue Wait Time ...
int qWTime;
public String toString() {
return "" + qETime + " : " + iWTime + " : " + eWTime + " : " + qWTime;
}
}
@viveknarang
Copy link
Author

Last few lines of the output ....

[0 : 0 : 5, 10 : 0 : 25, 30 : 0 : 15, 30 : 0 : 20]
At Time: 299 Length: 4
Discarding ... [299,5]
Discarding ... [299,1]
[0 : 0 : 5, 10 : 0 : 25, 30 : 0 : 15, 30 : 0 : 20]

--------------------------- RESULT ----------------------------

Maximum queue size: 3
Maximum wait time: 25
Maximum wait time: 5

@viveknarang
Copy link
Author

Last few lines of the new output ....

[0 : 0 : 5 : 0, 10 : 0 : 25 : 0, 30 : 0 : 15 : 5, 30 : 0 : 20 : 15]
At Time: 299 Length: 4
Discarding ... [299,5]
Discarding ... [299,1]
[0 : 0 : 5 : 0, 10 : 0 : 25 : 0, 30 : 0 : 15 : 5, 30 : 0 : 20 : 15]

--------------------------- RESULT ----------------------------

Maximum queue size: 3
Maximum wait time: 15
Maximum wait time: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment