Problem · Array

Escape Room Leaderboard

Learn this problem
MediumPinterest logoPinterestFULLTIMEPHONE SCREEN

Problem 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 room r to room r + 1 and return the new room as a decimal string. The operation is valid only when r < roomCount.
  • GET playerId: return the player's current room as a decimal string.
  • LEADERBOARD k: return up to k player 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 when k == 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 <= 100000
  • 1 <= roomCount <= 100000
  • 0 <= operations.length <= 200000
  • Player identifiers are unique nonempty ASCII strings without spaces or commas.
  • Every player referenced by an operation exists.
  • Every ADVANCE operation is valid.
  • 0 <= k <= players.length for every LEADERBOARD operation.

More Pinterest problems

drafts saved locally
public String[] runEscapeRoomLeaderboard(String[] players, int roomCount, String[] operations) {
  // write your code here
}
players["A","B"]
roomCount3
operations["ADVANCE A","ADVANCE B","ADVANCE A","LEADERBOARD 2","GET B"]
expected["1", "1", "2", "A", "B", "1"]
checking account