Problem · Array
Choose One from Each Set to Minimize the Range
Learn this problemProblem statement
You are given several non-empty integer sets, represented as an array sets. Every set contains the same number of integers. Choose exactly one integer from each set.
The range of a selection is the difference between its maximum and minimum selected values.
Return the minimum possible range over all valid selections.
Function
minimumSelectionRange(sets: int[][]) → longExamples
Example 1
sets = [[30,20,10,40],[22,33,44,99],[16,17,18,19]]return = 3Choose 20, 22, and 19. The range is 22 - 19 = 3, and no selection has a smaller range.
Example 2
sets = [[1,5],[4,8]]return = 1Choosing 5 from the first set and 4 from the second produces range 1.
Example 3
sets = [[-5],[10],[3]]return = 15Every set has only one choice, so the range is 10 - (-5) = 15.
Constraints
1 ≤ sets.length ≤ 1,0001 ≤ sets[i].lengthsets[i].length = sets[0].lengthfor every validi.- The total number of integers across all sets is at most
200,000. - Every value fits in a signed
32-bit integer.