Marathon Checkpoint Leaderboard
Problem statement
Maintain a live leaderboard for a marathon with sensorCount checkpoints numbered from 0 to sensorCount - 1 and playerCount runners numbered from 0 to playerCount - 1.
Before the first operation every runner is at checkpoint -1, ordered by runner ID. Process the operations in order:
["PASS", player, sensor]records thatplayerreached their next checkpoint. Thereforesensormust be exactly one greater than that runner's current checkpoint. Among runners at the same checkpoint, the runner who reached it earlier ranks first.["TOP", k]requests the firstkrunners, ranked by larger checkpoint first and then by arrival order at that checkpoint.
For each TOP operation, return one row containing entries of the form "player|checkpoint". Return query rows in encounter order.
Function
marathonLeaderboard(sensorCount: int, playerCount: int, operations: String[][]) → String[][]Examples
Example 1
sensorCount = 3playerCount = 4operations = [["PASS","2","0"],["PASS","0","0"],["TOP","4"],["PASS","0","1"],["TOP","3"]]return = [["2|0","0|0","1|-1","3|-1"],["0|1","2|0","1|-1"]]Runner 2 reaches checkpoint 0 before runner 0. After runner 0 advances to checkpoint 1, runner 0 leads.
Example 2
sensorCount = 2playerCount = 3operations = [["TOP","2"],["PASS","1","0"],["PASS","2","0"],["PASS","1","1"],["TOP","3"]]return = [["0|-1","1|-1"],["1|1","2|0","0|-1"]]The initial order is runner ID order. Later, runner 1 has progressed farther than runners 2 and 0.
Example 3
sensorCount = 1playerCount = 2operations = [["PASS","1","0"],["PASS","0","0"],["TOP","1"]]return = [["1|0"]]Both runners have reached the finish sensor, and runner 1 arrived first.
Constraints
1 <= sensorCount, playerCount <= 100000.1 <= operations.length <= 100000.- Every operation is
PASSorTOP. - Each
PASSuses a valid runner and that runner's next checkpoint. 1 <= k <= playerCountfor everyTOP.- At least one
TOPoperation is present.