Facing Heads Probabilties of Tossing Strange Coins using Dynamic

  • 时间:2020-09-18 17:01:02
  • 分类:网络文摘
  • 阅读:151 次

You have some coins. The i-th coin has a probability prob[i] of facing heads when tossed. Return the probability that the number of coins facing heads equals target if you toss every coin exactly once.

Example 1:
Input: prob = [0.4], target = 1
Output: 0.40000

Example 2:
Input: prob = [0.5,0.5,0.5,0.5,0.5], target = 0
Output: 0.03125

Constraints:
1 <= prob.length <= 1000
0 <= prob[i] <= 1
0 <= target <= prob.length
Answers will be accepted as correct if they are within 10^-5 of the correct answer.

Hints:
What about solving the problem with DP?
Use DP with two states dp[pos][cnt], where pos represents the pos-th coin and cnt is the number of heads seen so far.
You can do the transitions with a little bit math.
For the base case, when pos == n return (cnt == target) to filter out the invalid scenarios.

Depth First Search Algorithm to Calculate the Toss Probabilities

Each coin is tossed exactly once, and the probability of overall heads facing up stays the same regardless the order of the coins to toss. We can start at the first coin, imaging a binary tree where the root is the first coin, then the left tree is when the head faces up and the right tree when the head faces down. Therefore, the tree can be depth first search or breadth first search.

When we toss the last coin, we can check if the number of the heads up equal to the target, if yes, we accumulates the probability. We can speed up DFS by abandoning the branches where when we have tossed more than heads we wanted or the current probability is zero.

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
class Solution {
public:
    double probabilityOfHeads(vector<double>& prob, int target) {
        dfs(prob, 0, target, 1);
        return total;
    }
private:
    double total = 0;
    
    void dfs(vector<double>& prob, int index, int target, double cur) {
        if (target < 0) {
            return;
        }
        if (cur == 0) {
            return;
        }
        if (index >= prob.size()) {
            if (target == 0) {
                total += cur;
            }
            return;
        }
        dfs(prob, index + 1, target, cur * (1 - prob[index]));
        dfs(prob, index + 1, target - 1, cur * prob[index]);
    }
};
class Solution {
public:
    double probabilityOfHeads(vector<double>& prob, int target) {
        dfs(prob, 0, target, 1);
        return total;
    }
private:
    double total = 0;
    
    void dfs(vector<double>& prob, int index, int target, double cur) {
        if (target < 0) {
            return;
        }
        if (cur == 0) {
            return;
        }
        if (index >= prob.size()) {
            if (target == 0) {
                total += cur;
            }
            return;
        }
        dfs(prob, index + 1, target, cur * (1 - prob[index]));
        dfs(prob, index + 1, target - 1, cur * prob[index]);
    }
};

However, as the depth of the binary tree can go very deep, the performance may be hit greatly. The complexity is O(2^N) where N is the number of the coins – we may need to traverse all branches/nodes of the binary search tree.

The DFS is the TOP-Down search as we are search from the root (first coin) onwards.

Using Dynamic Programming Algorithm to Compute the Probabilties of Tossing a Coin

We can perform the calculations bottom-up using Dynamic Programming Algorithm. Let dp[i][j] represent the probability of getting j coins facing up after tossing the i-th coin. Therefore, we know that the probability of dp[i][j] can be accumulated from two parts: dp[i – 1][j] and dp[i – 1][j – 1].

For dp[i – 1][j], we have enough j coins facing up, thus for i-th coin, it has to face down – which is 1 – prob[i]. Similarly, for dp[i – 1][j – 1], we have to toss a facing-up coin, which is prob[i]

The initial values:

1
2
dp[0][1] = prob[0];
dp[0][1] = 1 - prob[0];
dp[0][1] = prob[0];
dp[0][1] = 1 - prob[0];

And we have to fill the dp[i][0] values which can be easily pre-computed:

1
dp[i][0] = dp[i - 1][0] * (1 - prob[i]);
dp[i][0] = dp[i - 1][0] * (1 - prob[i]);

The following C++ implements the DP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
    double probabilityOfHeads(vector<double>& prob, int target) {
        if (prob.empty()) return 0;
        vector<vector<double>> dp(prob.size(), vector<double>(prob.size() + 1, 0));
        dp[0][1] = prob[0];
        dp[0][0] = 1 - prob[0];
        for (int i = 1; i < prob.size(); ++ i) {
            dp[i][0] = dp[i - 1][0] * (1 - prob[i]);
            for (int j = 1; j <= target; ++ j) {
                dp[i][j] = dp[i - 1][j] * (1 - prob[i]) + dp[i - 1][j - 1] * prob[i];
            }
        }
        return dp[prob.size() - 1][target];
    }
};
class Solution {
public:
    double probabilityOfHeads(vector<double>& prob, int target) {
        if (prob.empty()) return 0;
        vector<vector<double>> dp(prob.size(), vector<double>(prob.size() + 1, 0));
        dp[0][1] = prob[0];
        dp[0][0] = 1 - prob[0];
        for (int i = 1; i < prob.size(); ++ i) {
            dp[i][0] = dp[i - 1][0] * (1 - prob[i]);
            for (int j = 1; j <= target; ++ j) {
                dp[i][j] = dp[i - 1][j] * (1 - prob[i]) + dp[i - 1][j - 1] * prob[i];
            }
        }
        return dp[prob.size() - 1][target];
    }
};

The space requirement is O(N^2) and the time complexity is also O(N^2) where N is the number of the coins.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
WordPress插件WP First Letter Avatar代码版  让WordPress文章内链接在新窗口打开的方法  如何让wrodpress在分类列表页显示其下子分类文章列表  如何下载WordPress官网插件的旧版本  剧场有多少座位  荷叶覆盖面积正好占池塘面积的四分之一  世界杯一共要进行多少场比赛  浓度问题  第二天生产的比总数的1/4少30个  这题出给小学生做合适吗? 
评论列表
添加评论