Guaranteed Left-and-Right Sock Pair
Problem statement
You have socks of several colors. For each color i, there are pairCounts[i] left socks and the same number of right socks.
For this exercise, assume socks are drawn one at a time without replacement in an arbitrary order. A usable pair consists of one left sock and one right sock of the same color. Two socks of the same side do not form a usable pair.
Return the minimum number of draws that guarantees at least one usable pair for every possible drawing order.
The report gives 12 red, 10 black, 8 green and 6 blue pairs. For this exercise, assume the per-color counts are an input generalizing those quantities. Complete minimumGuaranteedSockDraws(pairCounts) and return a signed 64-bit integer. The total pair count is positive.
Function
minimumGuaranteedSockDraws(pairCounts: int[]) → longExamples
Example 1
pairCounts = [12,10,8,6]return = 37Reported fixed case under stated assumptions. A pair-free prefix has at most 36 socks; one more guarantees a match.
Example 2
pairCounts = [1]return = 2Authored practice example. A pair-free prefix has at most 1 socks; one more guarantees a match.
Example 3
pairCounts = [0,2,0,1]return = 4Authored practice example. A pair-free prefix has at most 3 socks; one more guarantees a match.
Constraints
- For this exercise, assume
1 <= pairCounts.length <= 10^5. 0 <= pairCounts[i] <= 10^6.sum(pairCounts) > 0.- Each color has equally many left and right socks; drawing is without replacement.