We run a density report for a call center. The result should be displayed as a table with a row per day, showing the maximum number of active calls simultaneously during this day.
We create lib behind the interface. The contract indicates that we get the number of calls for that day and two arrays of integers, one with the start time and one with the end time of each call, therefore, for example:
During this day, only two calls are accepted: one goes from 20 to 30, and the other from 10 to 20. The maximum number of simultaneous calls is 1.
On the other hand, during another day, two calls are also accepted, one from 10 to 45, and the other from 15 to 40, then the maximum number of simultaneous calls is 2.
The contract for the web service is
public static int GetMaxDensity(int N, int[] X, int[] Y)
And the data looks like this (suppose 3 calls that were received that day). First one from 10 to 25, the second from 12 to 30 and the third from 20 to 23.
N = 3, X = {10, 12, 20} Y = {25, 30, 23}
And the return should be: 3.
I implemented this solution:
public static int GetMaxDensity(int N, int[] X, int[] Y) { int result = 0; for (int i = 0; i < N; i++) { int count = 0, t = X[i]; for (int j = 0; j < N; j++) { if (X[j] <= t && t < Y[j]) count++; } result = Math.max(count, result); } return result; }
And it works great when the number of calls reaches 1000 (weekends), but on working days the number is quite large, and the calculation takes so long (> 5 minutes). Now I can explain that I use two nested loops, but I don't have much experience with complex algorithms, so my question is:
Given that I just need the maximum number of simultaneous calls (not time and not calling), which may be a faster way to perform this calculation, if any.