Please Login in order to post a comment
Python 3 solutions with and without early exit:
def maxMin(k: int, arr: list[int]) -> int: arr.sort() offset = k - 1 min_unfairness = arr[offset] - arr[0] for i in range(k, len(arr)): if min_unfairness := min(arr[i] - arr[i - offset], min_unfairness): continue return 0 return min_unfairness def maxMin(k: int, arr: list[int]) -> int: k -= 1 arr.sort() return min(arr[i] - arr[i - k] for i in range(k, len(arr)))
python 3
def maxMin(k, arr): arr.sort() return min([arr[i+k-1]-arr[i] for i in range(len(arr) - k +1)])
My rust solution:
fn maxMin(k: i32, arr: &mut [i32]) -> i32 { arr.sort(); arr.windows(k as usize) .map(|window| window[(k as usize) -1] - window[0]) .min() .unwrap()
def maxMin(k, arr): arr.sort() diff = [] for i in range(len(arr)-k+1): diff.append(abs(arr[i]-arr[i+k-1])) return (min(diff))
def maxMin(k, arr):
dif = [] arr.sort() n = k - 1 for i in range(len(arr) - k + 1): dif.append(arr[n + i] - arr[i]) return min(dif)
Seems like cookies are disabled on this browser, please enable them to open this website
Python 3 solutions with and without early exit:
python 3
My rust solution:
def maxMin(k, arr):