1434. Switch Door Odds
A game show hides one prize behind one of doors closed doors. You pick a door, then the host, who knows where the prize is, opens exactly opened of the other doors, all of them empty. You must now switch, choosing uniformly at random among the doors that are still closed and are not your original pick. Return the probability that you win the prize, as a double. The prize is behind your first pick with probability 1 / doors, which means a switch loses in that case; otherwise it sits behind one of the doors - opened - 1 remaining candidates. Answers within an absolute error of 1e-9 are accepted. Use O(1) time and space.
Example 1
- Input:
- doors = 3, opened = 1
- Output:
- 0.6666666666666666
- Explanation:
With three doors and one opened, switching wins with probability 2/3.
Example 2
- Input:
- doors = 10, opened = 5
- Output:
- 0.225
- Explanation:
Nine of ten times the prize is elsewhere, and 4 doors remain to switch to, giving 9/(10*4) = 0.225.
Constraints
3 ≤ doors ≤ 106
1 ≤ opened ≤ doors - 2
Answers within an absolute error of 1e-9 are accepted.
How this problem is judged
- Answers
- Numbers are accepted within a tolerance of 1.0E-6: |answer - expected| <= 1.0E-6 x max(1, |expected|).
- Tolerance
0.000001
Expected complexity
- Time
- O(1)
- Space
- O(1)