A linear solution taking advantage of sorted indexes
This is an interesting problem: you'll be taking advantage that the indexes in an array by definition are already sorted. Hence, although you'll be using the fact that the indexes are sorted, you won't be sorting anything leading to a linear optimized solution. The idea is to keep a list of indexes for each value in the array. The list by definition will be sorted. Then do a linear scan of each list, calculating the distance and storing the overall min one. Code is down below, cheers, ACC. Minimum Distance Between Three Equal Elements II - LeetCode ou are given an integer array nums . A tuple (i, j, k) of 3 distinct indices is good if nums[i] == nums[j] == nums[k] . The distance of a good tuple is abs(i - j) + abs(j - k) + abs(k - i) , where abs(x) denotes the absolute value of x . Return an integer denoting the minimum possible distance of a go...