534. Game Play Analysis III

Database

Explanation

To solve this problem, we need to find the number of players who played at least three consecutive rounds and their player ID. We can iterate through the Activity table ordered by player_id and event_date, and use a window function to count the consecutive rounds played by each player. Then, we can filter out the players who played at least three consecutive rounds.

Algorithm:

  1. Use a window function to calculate the row number partitioned by player_id and ordered by event_date.
  2. Use a subquery to filter out the players who played at least three consecutive rounds.
  3. Select the distinct player_id from the subquery.

Time Complexity: O(n), where n is the number of records in the Activity table. Space Complexity: O(1)

# Write your Java solution here
SELECT DISTINCT player_id
FROM (
    SELECT player_id, event_date, 
           ROW_NUMBER() OVER (PARTITION BY player_id ORDER BY event_date) as row_num
    FROM Activity
) AS temp
WHERE temp.row_num >= 3

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.