Max Min Discussions | | HackerRank

Max Min

Sort by

recency

|

73 Discussions

|

  • + 0 comments

    I am ashamed how much time it took me to finally understand whats going on. I guess it is a good practice on how to gather the useful observations on what the questions is actually about.

  • + 0 comments

    These problems are poorly written.

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn max_min(k: i32, arr: &[i32]) -> i32 {
        //Time complexity: O(n*log(n))
        //Space complexity (ignoring input): O(n)
        let mut arr = arr.to_vec();
        let k = k as usize;
        arr.sort_unstable();
        let mut minimun_unfairness = arr[k - 1] - arr[0];
        for index in 1..(arr.len() - k + 1) {
            if minimun_unfairness > arr[k - 1 + index] - arr[index] {
                minimun_unfairness = arr[k - 1 + index] - arr[index];
            };
        }
        minimun_unfairness
    }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def max_min(k, arr):
        #Time complexity: O(n*log(n))
        #Space complexity (ignoring input): O(1)
        arr.sort()
        minimum_unfairness = arr[k - 1] - arr[0]
        for index in range(1, len(arr) - k + 1):
            if minimum_unfairness > arr[index + k - 1] - arr[index]:
                minimum_unfairness = arr[index + k - 1] - arr[index]
    
        return minimum_unfairness
    
  • + 0 comments

    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)))