TITLE: How do I approach this Combinatorics problem?
QUESTION [6 upvotes]: This is a question put up by Jane Street in their monthly Puzzle Archive.
The Problem
I'll write the problem here as well.

Jane received 78 figurines as gifts this holiday season:  12 drummers drumming, 11 pipers piping, 10 lords a-leaping, etc., down to 1 partridge in a pear tree. They are all mixed together in a big bag.  She agrees with her friend Alex that this seems like too many figurines for one person to have, so she decides to give some of her figurines to Alex. Jane will uniformly randomly pull figurines out of the bag one at a time until she pulls out the partridge in a pear tree, and will give Alex all of the figurines she pulled out of the bag (except the partridge, that’s Jane’s favorite).


If n is the maximum number of any one type of ornament that Alex gets, what is the expected value of n, to seven significant figures?

I approached the problem in this way. Please have a look. My Approach
I'll assume the following (in my approach) -

Let Ci - Selecting 'i' Identical candies before partridge is taken out.
Then I'll compute for each, C2 to C12 - for each type of figurine that fits the criteria.

Example C5 - Selecting 5 identical candies before partridge is taken out. It can be anything from type-5 figurine to type 12 (Drummers).


Is this the right way to solve it. Am i right in the fundamentals ? Is there a better way to go about the problem.

REPLY [3 votes]: The question is asking for $$\sum_{i=0}^{12}P(\text{max number of same figurine in 
sequence is } i | \text{sequence ends with a } 1)*i$$
If we can solve $P(\text{max number of same figurine in 
sequence is } i | \text{sequence ends with a } 1)$ for each $i$ then the answer is trivially found.
I'll consider a simpler case ($n=3$) for clarity. Note that these sequences have not been permuted.

max count of same figurine in sequence is $0$ given that sequence ends with a 1
$$1$$

max count of same figurine in sequence is $1$ given that sequence ends with a 1
$$21,31,321$$

max count of same figurine in sequence is $2$ given that sequence ends with a 1
$$221,3221,331,3321, 33221$$

max count of same figurine in sequence is $3$ given that sequence ends with a 1
$$3331, 33321, 333221$$


I'll use $P(i)$ to mean $P(\text{max number of same figurine in 
sequence is } i | \text{sequence ends with a } 1)$ from this point on.
$$P(0) = \frac{1}{\frac{n*(n+1)}{2}}\ (\frac{1}{6} \text{in this case})$$
$$P(1) = \frac{1!}{1!} \frac{2}{6}\frac{1}{6} + \frac{1!}{1!} \frac{3}{6}\frac{1}{6} + \frac{2!}{1!} \frac{2}{6}\frac{1}{6}$$
$$P(2) = \frac{2!}{2!} \frac{2}{6}\frac{1}{6}\frac{1}{6} + \frac{3!}{2!} \frac{3}{6}\frac{2}{6}\frac{1}{6}\frac{1}{6} + \frac{2!}{2!} \frac{3}{6}\frac{2}{6}\frac{1}{6}+\frac{3!}{2!} \frac{3}{6}\frac{2}{6}\frac{2}{6}\frac{1}{6}+\frac{4!}{2!2!} \frac{3}{6}\frac{2}{6}\frac{2}{6}\frac{1}{6}\frac{1}{6}$$
$$P(3) = \frac{3!}{3!} \frac{3}{6}\frac{2}{6}\frac{1}{6}\frac{1}{6} + \frac{4!}{3!}\frac{3}{6}\frac{2}{6}\frac{1}{6}\frac{2}{6}\frac{1}{6} + \frac{5!}{3!2!} \frac{3}{6}\frac{2}{6}\frac{1}{6}\frac{2}{6}\frac{1}{6}\frac{1}{6}$$
And so the expected value would just be $P(0)*1+P(1)*1+P(2)*2+P(3)*3=1.65$
This naive computation would not be feasible by hand for $n=12$ because you'd have to compute somewhere around $\frac{77!}{2!3!4!5!6!7!8!9!10!11!12!}$ sequences (and this is an underestimate since we can also have sequences with length less than $77$).
The question we should ask at this point is what can we do to avoid computing more than what we have to?
Let's just take a look at the sequence $333221$. The probability for getting this is $$\frac{5!}{3!2!}\frac{3}{6}\frac{2}{6}\frac{1}{6}\frac{2}{6}\frac{1}{6}\frac{1}{6}$$
Notice that we already know the probability of getting $221$ when we were computing $P(2)$ so the question is how can we refactor the probability of getting $333221$ in such a way that we can reuse the result from probability of getting $221$?
Suppose we were given probability of getting $221$, let's consider the spots we can put $3$ in.
$$\_2\_2\_1$$
Here is where we should consider how we can partition the number 3, because we can have either choose to put $333$ in one spot, $3$ in three spots or $3$ in one spot and $33$ in one spot (for example $3332\_2\_1, 323231, 332\_231$). Luckily, this is a known problem on leetcode.com.
Since $3$ is small, I'll just list out the possible ways of partitioning $3$, $[3] (\frac{1!}{1!}), [1,2](2!), [1,1,1] (\frac{3!}{3!}))$. So probability of getting $333221$ is
$$
\underbrace{\bigg({3\choose 1}\frac{1!}{1!}+{3\choose 2}2!+{3\choose 3}\frac{3!}{3!}\bigg)}_{\text{ways to partition } 3}*\underbrace{\frac{3}{6}\frac{2}{6}\frac{1}{6}}_{\text{probability to get } 3 \text{ 3 times}}*\underbrace{\frac{2!}{2!}\frac{2}{6}\frac{1}{6}\frac{1}{6}}_{\text{probability of getting } 221} $$
$$
\begin{align*}&=\bigg(\frac{3!}{2!}+\frac{3!*2!}{2!}+\frac{3!}{3!}\bigg)*\dots\\
&=10*\underbrace{\frac{3}{6}\frac{2}{6}\frac{1}{6}}_{\text{probability to get } 3 \text{ 3 times}}*\underbrace{\frac{2!}{2!}\frac{2}{6}\frac{1}{6}\frac{1}{6}}_{\text{probability of getting } 221}\\
&= \frac{5!}{3!2!}\frac{3}{6}\frac{2}{6}\frac{1}{6}\frac{2}{6}\frac{1}{6}\frac{1}{6}
\end{align*}$$
This brings us to our optimisation. Let p[i][j] be the array of probability of sequences with max number of any figurine at most i and we consider only all elements to the left of j. For clarity, here's a couple of examples for n=3:

p[3][2] is empty because we don't have $3$ $2$s
p[3][3] represents $3331, 33321, 333221$
p[2][3] represents $3221, 331, 3321, 33221$
p[2][2] represents $221$
p[2][1] is empty because we stop as soon as we draw $1$ and there are no elements left of $1$
p[1][3] represents $31, 321$
p[1][2] represents $21$
p[0][1] represents $1$
p[0][2] is empty since otherwise this would have max number of any figurine = 1.

Then p[i][j] can be formed by adding two parts:

probability of getting $j$ from $1$ to $i-1$ times, how many ways we can partition $i$ and probabilities from p[i][k] where $1\leq k\leq j-1$
probability of getting $j$ $i$ times, how many ways we can partition $i$ and probabilities from p[l][m] where $0\leq l \leq i$ and $1\leq m \leq j-1$

Note that in all cases we have p[0][1] being a length $1$ array (or whatever data structure you prefer) containing $\frac{1}{\frac{n(n+1)}{2}}$ as our base case.
Then $P(1) = \sum_{j=0}^{12}\sum_k p[1][j][k]$, $P(2) = \sum_{j=0}^{12}\sum_k p[2][j][k]$ and so on.