1230. Toss Strange Coins

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.

Rust Solution

struct Solution;

impl Solution {
    fn probability_of_heads(prob: Vec<f64>, target: i32) -> f64 {
        let n = prob.len();
        let mut dp = vec![vec![0.0; n + 1]; n + 1];
        dp[0][0] = 1.0;
        for i in 0..n {
            for j in 0..=i {
                dp[i + 1][j + 1] += dp[i][j] * prob[i];
                dp[i + 1][j] += dp[i][j] * (1.0 - prob[i]);
            }
        }
        dp[n][target as usize]
    }
}

#[test]
fn test() {
    use assert_approx_eq::assert_approx_eq;
    let prob = vec![0.4];
    let target = 1;
    let res = 0.4;
    assert_approx_eq!(Solution::probability_of_heads(prob, target), res);
    let prob = vec![0.5, 0.5, 0.5, 0.5, 0.5];
    let target = 0;
    let res = 0.03125;
    assert_approx_eq!(Solution::probability_of_heads(prob, target), res);
}

Having problems with this solution? Click here to submit an issue on github.