|
| 1 | +/** |
| 2 | + * Title: Probability of a Two Boxes Having the Same Number of Distinct Balls |
| 3 | + * Description: 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. |
| 4 | + * Author: Hasibul Islam |
| 5 | + * Date: 04/04/2023 |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * @param {number[]} balls |
| 10 | + * @return {number} |
| 11 | + */ |
| 12 | +var getProbability = function (balls) { |
| 13 | + var k = balls.length; |
| 14 | + var halfUsed = balls.reduce((acc, val) => acc + val, 0) / 2; |
| 15 | + var startArray = new Array(k); |
| 16 | + startArray.fill(0); |
| 17 | + |
| 18 | + const perm = function (b1, b2) { |
| 19 | + var p1, p2, s1, s2; |
| 20 | + |
| 21 | + s1 = b1.reduce((acc, val) => acc + val, 0); |
| 22 | + s2 = b2.reduce((acc, val) => acc + val, 0); |
| 23 | + |
| 24 | + const fact = function (n) { |
| 25 | + var f = 1; |
| 26 | + for (let i = 2; i <= n; i++) f *= i; |
| 27 | + return f; |
| 28 | + }; |
| 29 | + |
| 30 | + p1 = fact(s1); |
| 31 | + p2 = fact(s2); |
| 32 | + |
| 33 | + b1.forEach((val) => { |
| 34 | + if (val > 1) p1 /= fact(val); |
| 35 | + }); |
| 36 | + b2.forEach((val) => { |
| 37 | + if (val > 1) p2 /= fact(val); |
| 38 | + }); |
| 39 | + |
| 40 | + return p1 * p2; |
| 41 | + }; |
| 42 | + |
| 43 | + const getValidCombos = function (ballsUsed, colorNum = 0) { |
| 44 | + var box1Used = ballsUsed.reduce((acc, val) => acc + val, 0); |
| 45 | + var matches = { good: 0, total: 0 }, |
| 46 | + thisColorMax = halfUsed - box1Used; |
| 47 | + |
| 48 | + if (colorNum === k - 1) { |
| 49 | + /* |
| 50 | + Last ball color - adjust # of balls of this color to equal half |
| 51 | + (if possible). Then count # of different balls in each box. |
| 52 | + */ |
| 53 | + if (thisColorMax > balls[colorNum]) return { good: 0, total: 0 }; |
| 54 | + |
| 55 | + ballsUsed[colorNum] = thisColorMax; |
| 56 | + let ballsLeft = []; |
| 57 | + let colorsUsed = [0, 0]; |
| 58 | + for (let i = 0; i < k; i++) { |
| 59 | + ballsLeft[i] = balls[i] - ballsUsed[i]; |
| 60 | + if (ballsUsed[i] > 0) colorsUsed[0]++; |
| 61 | + if (ballsLeft[i] > 0) colorsUsed[1]++; |
| 62 | + } |
| 63 | + |
| 64 | + /* Count the # of permutations for the boxes represented by this 1 combination. */ |
| 65 | + let permutations = perm(ballsUsed, ballsLeft, k); |
| 66 | + return { |
| 67 | + good: colorsUsed[1] === colorsUsed[0] ? permutations : 0, |
| 68 | + total: permutations, |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + thisColorMax = Math.min(thisColorMax, balls[colorNum]); |
| 73 | + for (let i = 0; i <= thisColorMax; i++) { |
| 74 | + let match = getValidCombos([...ballsUsed], colorNum + 1); |
| 75 | + matches = { |
| 76 | + good: matches.good + match.good, |
| 77 | + total: matches.total + match.total, |
| 78 | + }; |
| 79 | + ballsUsed[colorNum]++; |
| 80 | + } |
| 81 | + return matches; |
| 82 | + }; |
| 83 | + |
| 84 | + /* Probability = (total # of permutations with equal # of balls) / (permutations with same # of unique balls) */ |
| 85 | + let res = getValidCombos(startArray); |
| 86 | + return res.good / res.total; |
| 87 | +}; |
| 88 | + |
| 89 | +console.log(getProbability([1, 2, 3, 4, 5])); |
0 commit comments