MediumNo. 0534LeetCode Premium

Game Play Analysis III — LeetCode 534 Python Solution

Topics
Database
Reading time
2 min

The problem

For every player and every date on which they logged in, report the running total of games they had played up to and including that date. The Activity table has player_id (int), device_id (int), event_date (date) and games_played (int), with (player_id, event_date) as its primary key. Return player_id, event_date and games_played_so_far.

Example

Input
Activity(player_id, device_id, event_date, games_played) = (1, 2, '2023-03-01', 5), (1, 2, '2023-05-02', 6), (1, 3, '2023-06-25', 1), (3, 1, '2023-03-02', 0), (3, 4, '2023-07-03', 5)
Output
(1, '2023-03-01', 5), (1, '2023-05-02', 11), (1, '2023-06-25', 12), (3, '2023-03-02', 0), (3, '2023-07-03', 5)
Explanation
Each row carries the sum of games_played for that player on every date up to and including its own.

Python solution

PythonSolution
import pandas as pd

def game_play_analysis(activity: pd.DataFrame) -> pd.DataFrame:
    df = activity.sort_values(['player_id', 'event_date'])
    df['games_played_so_far'] = df.groupby('player_id')['games_played'].cumsum()
    return df[['player_id', 'event_date', 'games_played_so_far']]

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

Frequently asked questions

How hard is LeetCode 534. Game Play Analysis III?

LeetCode 534. Game Play Analysis III is rated Medium on LeetCode.

What topics does LeetCode 534. Game Play Analysis III cover?

LeetCode 534. Game Play Analysis III is tagged Database on LeetCode.

Is LeetCode 534. Game Play Analysis III a premium problem?

Yes. LeetCode 534. Game Play Analysis III is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.