forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1467.cpp
More file actions
executable file
·47 lines (43 loc) · 1.15 KB
/
LC1467.cpp
File metadata and controls
executable file
·47 lines (43 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Problem Statement: https://leetcode.com/problems/probability-of-a-two-boxes-having-the-same-number-of-distinct-balls/
Time: O(n² + (n / k)ᵏ)
Space: O(n² + k)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
double getProbability(vector<int>& balls) {
int k, n;
double valid, total;
k = balls.size();
valid = total = 0;
n = accumulate(balls.begin(), balls.end(), 0);
double nCr[n + 1][n + 1];
// initialize nCr
for (int i = 0; i < n; i++)
nCr[i][0] = 1;
for (int i = 1; i < n; i++)
nCr[0][i] = 0;
for (int i = 1; i < n; i++)
for (int j = 1; j < n; j++)
nCr[i][j] = nCr[i - 1][j - 1] + nCr[i - 1][j];
// helper function
function<void(int, int, double, int, int)> state = [&](int pos, int taken, double ways, int box_1, int box_2) {
if (pos == k) {
if (taken == n / 2) {
valid += (box_1 == box_2) * ways;
total += ways;
}
return;
}
for (int i = 0; i <= balls[pos]; i++)
state(pos + 1,
taken + i,
ways * nCr[balls[pos]][i],
box_1 + (i > 0),
box_2 + (balls[pos] - i > 0));
};
state(0, 0, 1, 0, 0);
return valid / total;
}
};