Sherlock and MiniMax Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hi, in Java, I always get:
"Time limit exceeded Your code did not execute within the time limits. Please optimize your code."
for test case 8, 9 and 10. If I run the test locally with the test data from 8, 9 and 10, it runs perfectly. I have done some optimisation (e.g. convert list to array), but cant see how to optimise further.
Here is my code:
public static int sherlockAndMinimax(List arr, int p, int q) {
int M;
int maxMin = Integer.MIN_VALUE;
int maxMinPos = 0;
int[] optArr = arr.parallelStream().mapToInt(Integer::intValue).toArray();
int size = arr.size();
for (M = p; M <= q; M++) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < size; i++) {
int diff = Math.abs(optArr[i] - M);
if (diff < min) {
min = diff;
}
}
if (min > maxMin) {
maxMin = min;
maxMinPos = M;
}
}
return maxMinPos;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and MiniMax
You are viewing a single comment's thread. Return to all comments →
Hi, in Java, I always get: "Time limit exceeded Your code did not execute within the time limits. Please optimize your code."
for test case 8, 9 and 10. If I run the test locally with the test data from 8, 9 and 10, it runs perfectly. I have done some optimisation (e.g. convert list to array), but cant see how to optimise further. Here is my code: public static int sherlockAndMinimax(List arr, int p, int q) { int M; int maxMin = Integer.MIN_VALUE; int maxMinPos = 0;