Problem · Array
Escape Room Leaderboard
Learn this problemProblem statement
Players progress through escape rooms numbered from 0 through roomCount. Every player in players begins in room 0. Their initial entry order into room 0 is their order in players.
Process operations in order:
ADVANCE playerId: move the player from roomrto roomr + 1and return the new room as a decimal string. The operation is valid only whenr < roomCount.GET playerId: return the player's current room as a decimal string.LEADERBOARD k: return up tokplayer identifiers, ordered by decreasing room number. Players in the same room are ordered by when they entered that room; earlier entry comes first. Encode the identifiers as one comma-separated string, or an empty string whenk == 0.
Return one result string for every operation. The intended data structure supports ADVANCE and GET in constant time and LEADERBOARD in O(roomCount + k) time.
Function
runEscapeRoomLeaderboard(players: String[], roomCount: int, operations: String[]) → String[]Examples
Example 1
players = ["A","B"]roomCount = 3operations = ["ADVANCE A","ADVANCE B","ADVANCE A","LEADERBOARD 2","GET B"]return = ["1","1","2","A,B","1"]A reaches room 2 while B remains in room 1, so the leaderboard is A,B.
Example 2
players = ["A","B","C"]roomCount = 2operations = ["ADVANCE A","ADVANCE B","LEADERBOARD 3"]return = ["1","1","A,B,C"]A entered room 1 before B, so that tie keeps A first. C follows from room 0.
Example 3
players = ["x","y"]roomCount = 1operations = ["GET y","LEADERBOARD 1","LEADERBOARD 0"]return = ["0","x",""]Both players remain in room 0, where the original player order breaks the tie. A request for zero players returns an empty string.
Constraints
1 <= players.length <= 1000001 <= roomCount <= 1000000 <= operations.length <= 200000- Player identifiers are unique nonempty ASCII strings without spaces or commas.
- Every player referenced by an operation exists.
- Every
ADVANCEoperation is valid. 0 <= k <= players.lengthfor everyLEADERBOARDoperation.