Probability of a Two Boxes Having The Same Number of Distinct Balls Problem
Description
LeetCode Problem 1467.
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.
All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully).
Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully).
Return the probability that the two boxes have the same number of distinct balls. Answers within 10^(-5) of the actual value will be accepted as correct.
Example 1:
Input: balls = [1,1]
Output: 1.00000
Explanation: Only 2 ways to divide the balls equally:
- A ball of color 1 to box 1 and a ball of color 2 to box 2
- A ball of color 2 to box 1 and a ball of color 1 to box 2
In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1
Example 2:
Input: balls = [2,1,1]
Output: 0.66667
Explanation: We have the set of balls [1, 1, 2, 3]
This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equal probability (i.e. 1/12):
[1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1]
After that, we add the first two balls to the first box and the second two balls to the second box.
We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box.
Probability is 8/12 = 0.66667
Example 3:
Input: balls = [1,2,1,2]
Output: 0.60000
Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box.
Probability = 108 / 180 = 0.6
Constraints:
1 <= balls.length <= 81 <= balls[i] <= 6sum(balls)is even.
Solution
Approach: Count Favorable Splits with Memoized Search
Give every ball its own label. A uniform shuffle followed by "first half to box 1" is the same as choosing a uniformly random set of n of the 2n balls for box 1, so there are C(2n, n) equally likely outcomes, and the answer is the number of good outcomes divided by C(2n, n).
Count the good outcomes color by color. If x of the balls[i] balls of color i go to box 1, there are C(balls[i], x) ways to choose which ones. What matters for the "distinct colors" condition is only:
x == 0: coloriis only in box 2,x == balls[i]: coloriis only in box 1,- anything in between: it is in both boxes and does not change the comparison.
So define ways(i, left, diff) as the number of ways to place colors i, i+1, ... when box 1 still needs left balls and diff is (colors only in box 1) minus (colors only in box 2) so far. It tries every x from 0 to min(balls[i], left) and adds C(balls[i], x) * ways(i + 1, left - x, diff + d). At the end, a split is good when left == 0 and diff == 0. Memoizing on (i, left, diff) keeps the search tiny.
With 8 colors of 6 balls, C(48, 24) is about 3.2 · 1013, so the counts fit in 64-bit integers (and exactly in a JavaScript number).
Complexity: O(k2 · n · b) time for k colors, n balls per box and at most b balls per color, which is only a few thousand steps here, and O(k2 · n) space for the memo.
from functools import lru_cache
class Solution:
def getProbability(self, balls: List[int]) -> float:
k = len(balls)
half = sum(balls) // 2
size = max(sum(balls), max(balls))
# Pascal's triangle: comb[a][b] = C(a, b)
comb = [[0] * (size + 1) for _ in range(size + 1)]
for a in range(size + 1):
comb[a][0] = 1
for b in range(1, a + 1):
comb[a][b] = comb[a - 1][b - 1] + comb[a - 1][b]
@lru_cache(None)
def ways(i: int, left: int, diff: int) -> int:
"""Ways to pick `left` more balls for box 1 from colors i.. so that the
distinct-color counts end up equal; diff = colors only in box 1 minus only in box 2."""
if i == k:
return 1 if left == 0 and diff == 0 else 0
total = 0
for x in range(min(balls[i], left) + 1): # x balls of color i go to box 1
if x == 0:
d = -1 # the color is only in box 2
elif x == balls[i]:
d = 1 # the color is only in box 1
else:
d = 0 # the color is in both boxes
total += comb[balls[i]][x] * ways(i + 1, left - x, diff + d)
return total
return ways(0, half, 0) / comb[2 * half][half]