TLE when using java 8 lambda, any better solution?


  • 0
    F

    Hashmap solutions are trivial. I tried to solve it using java 8 lambda but can't get an O(n) solution, below O(n2) one got TLE. Do you have any better idea?

    public boolean containsNearbyDuplicate(int[] a, int k) {
    		if (a == null) {
    			return false;
    		}
    		int n = a.length;
    		return IntStream.range(0, n).
    				parallel()
    				.anyMatch(
    						i -> IntStream.range(i+1, n).anyMatch(
    								j -> a[i] == a[j] && j - i <= k));
    	}

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.