Four-Player Matching Card Game
Problem statement
Cards are encoded as rank-suit, where rank is an integer and suit is a nonempty uppercase token. deckOrder is the supplied deck from top to bottom.
- Deal
handSizecards to each of four players in round-robin order, then sort every hand by rank and suit. - Draw the next deck card as the current card.
- Players take turns
0,1,2,3. A player plays the first card in their sorted hand whose rank or suit matches the current card. That card becomes current. - If no card matches, draw the next deck card into the hand, sort the hand, and end the turn without immediately playing it.
- Return the first player whose hand becomes empty. If the draw pile is empty and a full round passes with no play, return
-1.
Function
matchingCardGameWinner(deckOrder: String[], handSize: int) → intExamples
Example 1
deckOrder = ["1-A","2-B","3-C","4-D","5-A"]handSize = 1return = 0Player 0 plays 1-A on 5-A and empties their hand.
Example 2
deckOrder = ["1-A","2-B","3-C","4-D","9-B","8-A"]handSize = 1return = 1Player 0 draws 8-A and ends the turn; player 1 then plays 2-B on 9-B.
Example 3
deckOrder = ["1-A","2-B","3-C","4-D","9-E"]handSize = 1return = -1No hand matches rank 9 or suit E, and no draw cards remain.
Constraints
1 <= handSizeand4*handSize+1 <= deckOrder.length <= 200000.- Every card matches
[1-9][0-9]*-[A-Z]+. - The supplied deck order is deterministic; duplicate card codes are allowed.