Created
August 13, 2017 03:59
-
-
Save tanglie1993/0c649e0bdf0bf846104469902259dc59 to your computer and use it in GitHub Desktop.
NumberOfTasksRunning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int[] numberOfTasks(int[] start, int[] end, int[] queries){ | |
if(start == null || end == null || queries == null){ | |
throw new IllegalArgumentException("input cannot be null!"); | |
} | |
if(start.length != end.length){ | |
throw new IllegalArgumentException("Length of start and end must be identical!"); | |
} | |
Arrays.sort(start); | |
Arrays.sort(end); | |
int[] sortedQueries = Arrays.copyOf(queries, queries.length); | |
Arrays.sort(sortedQueries); | |
Map<Integer, Integer> resultMapping = new HashMap<>(); | |
int[] result = new int[queries.length]; | |
int startIndex = 0; | |
int endIndex = 0; | |
for (int sortedQuery : sortedQueries) { | |
for (; startIndex < start.length; startIndex++) { | |
if (start[startIndex] > sortedQuery) { | |
break; | |
} | |
} | |
for (; endIndex < end.length; endIndex++) { | |
if (end[endIndex] > sortedQuery) { | |
break; | |
} | |
} | |
resultMapping.put(sortedQuery, startIndex - endIndex); | |
} | |
for(int i = 0; i < queries.length; i++){ | |
result[i] = resultMapping.get(queries[i]); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment