1433. Coupon Album Finish

Easy1.5 points estimatedProbabilityMaths

A sticker album has total different sticker kinds. You already own owned distinct kinds, and every pack you open contains exactly one sticker whose kind is chosen uniformly at random from all total kinds. Return the expected number of packs you must open until the album is complete, as a double. When you are missing m = total - owned kinds, the answer is total times the sum of 1/k for k from 1 to m, and it is 0 when the album is already complete. Answers within an absolute or relative error of 1e-6 are accepted. Aim for O(total) time and O(1) space.

Example 1

Input:
total = 5, owned = 0
Output:
11.416666666666666
Explanation:

Starting from nothing with 5 kinds, the expected number of packs is 5 * (1 + 1/2 + 1/3 + 1/4 + 1/5) = 11.4166...

Example 2

Input:
total = 100, owned = 100
Output:
0
Explanation:

The album is already complete, so no more packs are needed.

Constraints

1 ≤ total ≤ 106

0 ≤ owned ≤ total

Absolute or relative error of 1e-6 is accepted.

How this problem is judged

Answers
Numbers are accepted within a tolerance of 1.0E-6: |answer - expected| <= 1.0E-6 x max(1, |expected|).
Tolerance
0.000001

Expected complexity

Time
O(total - owned)
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…