leetcode-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:
1 | Input: prob = [0.4], target = 1 |
Example 2:
1 | Input: prob = [0.5,0.5,0.5,0.5,0.5], target = 0 |
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.
分析
$dp[i][j]$ 表示从 $0…i$ 个硬币中选出 $j$ 个正面朝上的概率。那个对于编号为 $i$ 的硬币就有两种状态,正面朝上和正面朝下。于是有状态转移方程:
$$
dp[i][j] = prob[i]dp[i-1][j-1] + (1-prob[i])dp[i-1][j]
$$
目标:$dp[n-1][target]$
代码
1 | // 经过降为之后的代码 |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/27/leetcode-1230/
License: 知识共享署名-非商业性使用 4.0 国际许可协议