Problem 291112 · easy · Level 02 Linear Data Structures

Pass the Parcel Knockout

queues · deque · simulation

At a birthday party the players sit in a circle in the order given by names, and the parcel starts with names[0]. Each round the parcel is handed on k times, always to the next player round the circle, and whoever holds it after the k-th hand-on is out. The next round begins with the player who sat just after the one who left.

Return the players in the order they went out, with the winner (the last player left) as the final entry.

Examples

Input:  names = ["Ana", "Ben", "Cleo", "Dev", "Eli", "Fay"], k = 8
Output: ["Cleo", "Ana", "Ben", "Fay", "Dev", "Eli"]
Explanation: 8 passes around 6 players go once round the circle and 2 more, landing on Cleo; then 8 passes among the remaining 5 (starting from Dev) land on Ana, and so on. Eli wins.

Input:  names = ["a", "b", "c"], k = 1
Output: ["b", "a", "c"]

Constraints

  • 1 <= len(names) <= 5000
  • 0 <= k <= 10**9
  • Target: O(n * min(k, n)) with the modulo trick, or better

Goals

  • Model a circle of players with a rotating queue
  • Use modulo to avoid passing more times than there are players
Starting Python…