> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Toss Strange Coins Python Solution with Tests

> Tested Python solution for LeetCode 1230 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1230, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming), Probability and Statistics. [View on LeetCode](https://leetcode.com/problems/toss-strange-coins/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1230   # by problem number
lcpy gen -s toss_strange_coins   # by problem name
```

## Problem

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.

### Examples

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

```
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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/toss_strange_coins/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/toss_strange_coins/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * target)
    # Space: O(target)
    def probability_of_heads(self, prob: list[float], target: int) -> float:
        f = [0.0] * (target + 1)
        f[0] = 1.0
        for p in prob:
            for j in range(target, -1, -1):
                f[j] *= 1 - p
                if j:
                    f[j] += p * f[j - 1]
        return f[target]
```

## Complexity

| Time | Space |
| - | - |
| O(n \* target) | O(target) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.