NewThe Learning module is now liveSee what's new

You are building a real-time leaderboard for an Escape Room Center that has nn sequential rooms labeled 00 to n−1n - 1. When the game begins, each of mm participants starts in room 0 and can only move to the next room after solving its puzzle. Even if several players occupy the same room, they all solve independently at their own pace.

Your leaderboard needs to track players' progress and support fast queries about room populations and top performers. In particular, whenever a player advances to the next room.

Implement the EscapeRoom class:

  • EscapeRoom(int numRooms, int maxParticipants) Initialize the game with numRooms rooms and capacity for maxParticipants players.

  • void registerParticipant(String participantId) Register a new participant in room 0.

  • boolean increment(String participantId) Advance participantId player from their current room to the next room. Return false if the ID doesn't exist or is already in the last room. Otherwise, return true.

  • int countParticipantsInRoom(int room)
    Return the number of players currently in room. If room is invalid (less than 0 or ≥ numRooms), it returns 0.

  • String[] getTopParticipants(int k)
    Return up to k participant IDs who have made the most progress in the game. If multiple participants are in the same room, prioritize those who entered the room earlier. If k exceeds the total number of registered participants, only the available IDs are returned.

Constraints:

  • 11 ≤ numRooms ≤ 10410⁴
  • 00 ≤ maxParticipants ≤ 10510⁵
  • Total number of method calls ≤ 10510⁵

Example

Input: ["EscapeRoom", "registerParticipant", "registerParticipant", "registerParticipant", "registerParticipant", "increment", "increment", "increment", "increment", "increment", "countParticipantsInRoom", "countParticipantsInRoom", "countParticipantsInRoom", "countParticipantsInRoom", "getTopParticipants"] [[4, 4], ["P0"], ["P1"], ["P2"], ["P3"], ["P2"], ["P2"], ["P2"], ["P0"], ["P3"], [0], [1], [2], [3], [2]]

Output: [null, null, null, null, null, true, true, true, true, true, 1, 2, 0, 1, ["P2", "P0"]]

Explanation: Initial state:

final state:

  • EscapeRoom room = new EscapeRoom(4, 4); // Create 4-room game with maximum of 4 participants.
  • room.registerParticipant("P0"); // register participant P0
  • room.registerParticipant("P1");
  • room.registerParticipant("P2");
  • room.registerParticipant("P3");
  • room.increment("P2"); // Returns true. "P2" → room 1
  • room.increment("P2"); // Returns true. "P2" → room 2
  • room.increment("P2"); // Returns true. "P2" → room 3
  • room.increment("P0"); // Returns true. "P0" → room 1
  • room.increment("P3"); // Returns true. "P3" → room 1
  • room.countParticipantsInRoom(0); // Returns 1. Only "P1" is in room 0.
  • room.countParticipantsInRoom(1); // Returns 2. "P0" and "P3" are in room 1.
  • room.countParticipantsInRoom(2); // Returns 0.
  • room.countParticipantsInRoom(3); // Returns 1. Only "P2" is in room 3.
  • room.getTopParticipants(2); // Returns ["P2", "P0"], as "P2" (in room 3) made most progress in the game. "P0" and "P3" are both in room 1, and "P0" is prioritized because it entered the room earlier.

Accepted
2.7k/7.8k
Acceptance Rate34.7%
Test Cases
Input
Output
Expected