1530. Guaranteed Pairs Draw
A warehouse bin holds an endless supply of socks in colours different colours. A worker pulls socks out one at a time without looking, and wants to be certain of holding pairs disjoint matching pairs, where a pair is two socks of the same colour and no sock belongs to two pairs.
Return the smallest number of socks the worker must draw so that this is guaranteed no matter which colours come out, as a long. Think about the unluckiest sequence of draws: how many socks can be held while still having fewer than pairs disjoint pairs?
Input: two integers colours and pairs. Output: one integer. A closed form works in O(1) time and O(1) space.
Example 1
- Input:
- colours = 3, pairs = 1
- Output:
- 4
- Explanation:
With 3 colours, 3 draws can all differ, so the 4th sock must complete a pair.
Example 2
- Input:
- colours = 2, pairs = 2
- Output:
- 5
- Explanation:
The worst case holds one pair plus one sock of each colour, 4 socks, so the 5th sock forces the second pair.
Constraints
1 ≤ colours ≤ 109
1 ≤ pairs ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(1)
- Space
- O(1)