Sherlock and MiniMax Discussions | Algorithms | HackerRank

Sherlock and MiniMax

  • + 0 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;

        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;
    }