Problem Statement
You and several friends are playing a tennis game called king of the court. The idea behind the game is that one person is the "king of the court", and each of the other players, in turn, tries to win two points before the king wins one point. An opponent who successfully scores two points without allowing the king to score becomes the new king of the court; otherwise, he returns to the back of the line to wait to play again. When the king is defeated, the winner becomes the new king, and the king returns to the back of the line. A player is declared the winner when he or she has been the king and consecutively defeated each of the other opponents.
You are given a
On any given turn, the probability that a player A will win the point against player B is based upon their relative skill level. Thus, if two players have skill levels A and B, the probability that the first player will win the point (regardless of who is king) is A/(A+B).
You are to return a
Definition
- Class:
- KingOfTheCourt
- Method:
- chancesOfWinning
- Parameters:
- int[]
- Returns:
- double
- Method signature:
- double chancesOfWinning(int[] ability)
- (be sure your method is public)
Notes
- At the start of the game, the king has not yet defeated any opponents.
- Your return must have an absolute or relative error less than 1e-9.
Constraints
- ability will contain between 2 and 7 elements, inclusive.
- Each element of ability will be between 1 and 99, inclusive.
Examples
{ 1, 1 }
Returns: 0.8
Here, it is just you and a friend of the same ability. So, on any given point either of you has a 50% chance of winning. Thus, on each round, whoever is king has 75% odds, and the non-king has 25% odds. Thus, there is a 75% chance you win the game on the first round, or a 25% chance that you go to another round, at which point there's a 25% chance of you once again becoming king. This suggests the following equation, where p is the probability that you win the game: p = .75p + .25 * .25p Solving, we get p = 80%
{ 2, 1 }
Returns: 0.9350649350649349
Here, you are twice as good as your friend, so the chances of you winning a round as king are 8/9. Your chances of winning while not king are 4/9. In an equation similar to above: p = (8/9) * p + (1/9) * (4/9) * p Solving, we get p = 72 / 77
{ 1, 2 }
Returns: 0.5844155844155844
Here, your friend is better than you. But, starting as king gives you some advantage. p = (5/9) * p + (4/9) * (1/9) * p Solving, we get p = 45 / 77
{ 1, 1, 1, 1 }
Returns: 0.5065812082824602
Now it's you with three other friends. Note that even though you're all of equal ability, starting as king provides a serious advantage.
{ 47, 82, 65, 99, 2, 14, 9 }
Returns: 0.22734781036506918
{39,13,54}
Returns: 0.6762153623688765
{86,2}
Returns: 0.9999767767581795
{89,46,1,91,18,36,12}
Returns: 0.6868719469373123
{39,41,59,32,97,42}
Returns: 0.16379671009097374
{35,69}
Returns: 0.5891924484230299
{8,71,70}
Returns: 0.037826189197781315
{60,29,55}
Returns: 0.7692771265722392
{65,96,55,79,97}
Returns: 0.28015674257401985
{94,3,60,33,19,22,22}
Returns: 0.8428053892425473
{14,85}
Returns: 0.2667629127540412
{71,42,91,40,20,50}
Returns: 0.511518638371499
{18,50,53,57}
Returns: 0.0925390880924591
{99,40,94}
Returns: 0.7747150192386446
{16,74}
Returns: 0.3310234290528628
{99,62}
Returns: 0.902297361097626
{16,71,58,73}
Returns: 0.04401935785171435
{65,49,85,99,82,75}
Returns: 0.2258693317308275
{15,57}
Returns: 0.3837013673079247
{24,10,19,14,6,69}
Returns: 0.30833921892246624
{72,89,91,93,94,96,99}
Returns: 0.1403814777824161
{ 47, 82, 65, 99, 2, 14, 9 }
Returns: 0.22734781036506918
{ 50, 2, 30, 4, 5, 6, 7 }
Returns: 0.8971810034837033