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.
Here's my Java 8 solution, passing all tests, for Max Score of 35:
Code is fairly simple & self-explanatory. First sort the List. Then iterate through it once, doing a "sliding sublist window", calculate Current Diff by considering the first / last element of the sub-list as Min / Max, and compare that with the "Overall Min Diff" & update as needed.
`
public static int maxMin(int k, List<Integer> arr) {
// Write your code here
int minUnfairness = Integer.MAX_VALUE;
Collections.sort(arr);
for (int i = 0 ; i <= (arr.size() - k) ; i++) {
List<Integer> curSubLst = arr.subList(i, (i + k));
int curUnfairness = curSubLst.get(curSubLst.size() - 1) - curSubLst.get(0);
minUnfairness = Math.min(minUnfairness, curUnfairness);
}
return minUnfairness;
}
Max Min
You are viewing a single comment's thread. Return to all comments →
Here's my Java 8 solution, passing all tests, for Max Score of 35:
Code is fairly simple & self-explanatory. First sort the List. Then iterate through it once, doing a "sliding sublist window", calculate Current Diff by considering the first / last element of the sub-list as Min / Max, and compare that with the "Overall Min Diff" & update as needed.
`
`
This problem's solution approach is very similar / identical to the "Minimum Absolute Difference in an Array" problem: as explained in this comment I have posted in the discussions of that problem.