Spread of Every Selection
A selection picks one or more positions of nums. Two selections are different when they pick different sets of positions, even if the values are the same. The spread of a selection is its largest picked value minus its smallest, so a selection of one position has spread 0.
Return the sum of the spreads of all selections.
Example 1
nums [4,1,2]
returns 9
The pairs have spreads 3 (4 and 1), 2 (4 and 2) and 1 (1 and 2), and all three together have spread 3. Single items add 0.
Example 2
nums [5,5,5]
returns 0
Every selection has spread 0, because all the values are equal.
Constraints
1 <= len(nums) <= 300 <= nums[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Swaps for Two Rising ListsHard
- Two Trades at MostHard
- Build Array from PermutationBeginner
- Close NeighborsBeginner
- Concatenation of ArrayBeginner
- Deal Into PilesBeginner
- Decompress a Pair ListBeginner
- Find the Highest AltitudeBeginner