Ctrl K

Alternating Team Picks

Easy
Players stand in a queue: names[i] is a name and teams[i] is a team (0 or 1). All names are distinct. Call as many players onto the field as possible, keeping the queue order (any player may be skipped), so that any two players called one after another are from different teams. Return the names of the called players in order. If several longest selections exist, pick the one that calls players as early as possible: at every step, take the earliest suitable player.
Example 1
names=["ann", "bob", "cid", "dan", "eve"], teams=[1, 1, 0, 1, 1] → ['ann', 'cid', 'dan']

At most three players can alternate. Among the options (ann/bob, then cid, then dan/eve) the earliest ones are taken: ann, cid, dan.

Example 2
names=["x", "y", "z"], teams=[0, 1, 0] → ['x', 'y', 'z']

The teams already alternate, so everyone is taken.

Example 3
names=["solo", "duo"], teams=[0, 0] → ['solo']
Constraints
  • 1 ≤ len(names) = len(teams) ≤ 100
  • teams[i] is 0 or 1
  • 1 ≤ len(names[i]) ≤ 10, lowercase English letters
  • All names are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBead String Without Twins MediumWord From a Prefix Table HardRise and Fall Lineup EasyDoes the Text Fit the Pattern HardFewest Jumps to the End Medium
loading Python…
Input
names=["ann", "bob", "cid", "dan", "eve"], teams=[1, 1, 0, 1, 1]
Expected
['ann', 'cid', 'dan']

Run checks the first 3 tests in your browser. Submit checks all 195 tests on the server, hidden ones included.