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.
- Prepare
- Algorithms
- Greedy
- Max Min
- Discussions
Max Min
Max Min
Sort by
recency
|
885 Discussions
|
Please Login in order to post a comment
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.
Here is my Python solution!
Java:
def maxMin(k, arr): return min(map(lambda x, y: abs(x - y), sorted(arr)[:-k + 1], sorted(arr)[k - 1:]))
JS