Connection Status:
Competition Arena > MusicalChairs
TC China 08 - 1B · 2008-11-23 · by Eeyore · Simple Math
Class Name: MusicalChairs
Return Type: String
Method Name: knockedOut
Arg Types: (vector<string>, string, int)
Problem Statement

Problem Statement

In the game of Musical Chairs, n players walk around a circle of n-1 chairs while music plays on the stereo. When the music stops, everyone hurries to claim a chair. The player who is left standing gets knocked out of the game.

You are given a String[] playerNames containing the names of the players, ordered according to their position at the start of the game, such as the following.

  {"Emma Sue", "Billy Bob Thornton", "Joe", "Cassius Clay"}

You are also given a String chairs describing the arrangement of chairs with an 'h' (lowercase letter H) for each chair and exactly one '.' (period) indicating a gap. The i-th character of chairs corresponds to the i-th element of playerNames. For instance, the chairs might be arranged as follows.

  "hh.h"

The player by the gap is the one who gets knocked out. For example, if the four players above are in their starting order when the music stops, then Joe gets knocked out.

For each second of music, the players advance one position. From the original order shown above, for example, the players' order changes to the following after one second.

  {"Cassius Clay", "Emma Sue", "Billy Bob Thornton", "Joe"}

Given an int seconds specifying the number of seconds that the music is played, return the name of the player who gets knocked out.

Constraints

  • playerNames will contain between 2 and 50 elements, inclusive.
  • Each element of playerNames will contain between 1 and 50 characters, inclusive.
  • playerNames will contain only letters ('A'-'Z', 'a'-'z') and spaces (' ').
  • chairs will contain the same number of characters as the number of elements in playerNames.
  • Exactly one character in chairs will be '.', and all the other characters will be 'h'.
  • seconds will be between 0 and 600, inclusive.
Examples
0)
{"Emma Sue", "Billy Bob Thornton", "Joe", "Cassius Clay"}
"hh.h"
1
Returns: "Billy Bob Thornton"

The example from the problem statement.

1)
{"Emma Sue", "Billy Bob Thornton", "Joe", "Cassius Clay"}
"hh.h"
4
Returns: "Joe"

After 4 seconds, the players are exactly at the same positions as in the beginning.

2)
{"Jack Dempsey", "Joe Louis", "Rocky Marciano",
 "Cassius Clay", "George Foreman", "Mike Tyson"}
"h.hhhh"
500
Returns: "Mike Tyson"
3)
{"  itchy   ", "SCRATCHY"}
".h"
0
Returns: "  itchy   "
4)
{"  itchy   ", "SCRATCHY"}
".h"
600
Returns: "  itchy   "

Submissions are judged against all 69 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class MusicalChairs with a public method string knockedOut(vector<string> playerNames, string chairs, int seconds) · 69 test cases · 2 s / 256 MB per case

Submitting as anonymous