1430. Dark Drawer Draws

Easy1.5 points estimatedBrainteaserMathsCounting

A sock drawer holds counts[c] socks of colour c and the light is off, so socks are pulled out blindly one at a time. Return the smallest number of draws that guarantees you hold two socks of the same colour, no matter how unlucky the draws are. If no colour has at least two socks, such a pair can never be made and the answer is -1. By the pigeonhole principle, the worst case takes one sock of every colour that has at least one sock, so the answer is that number of colours plus one. The expected running time is O(n) with O(1) extra space.

Example 1

Input:
counts = [3,5,2]
Output:
4
Explanation:

Three colours are present, so the worst case is one sock of each colour and the fourth draw must complete a pair.

Example 2

Input:
counts = [1,1,1]
Output:
-1
Explanation:

Every colour has a single sock, so no pair can ever be drawn and the answer is -1.

Constraints

1 ≤ n = counts.length ≤ 1000
0 ≤ counts[c] ≤ 106

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…