EasyNo. 0511

Game Play Analysis I — LeetCode 511 Python Solution

Topics
Database
Reading time
2 min

The problem

For every player, find the date they first logged in. 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, and each row records one day on which a player logged in and played some number of games. Return player_id and first_login.

Example

Input
Activity(player_id, device_id, event_date, games_played) = (1, 2, '2023-01-03', 5), (1, 2, '2023-01-06', 2), (2, 3, '2023-02-11', 1), (3, 1, '2023-01-05', 0), (3, 4, '2023-03-02', 4)
Output
(1, '2023-01-03'), (2, '2023-02-11'), (3, '2023-01-05')
Explanation
The minimum event_date per player is that player's first login date.

Python solution

PythonSolution
import pandas as pd


def game_analysis(activity: pd.DataFrame) -> pd.DataFrame:
    return (
        activity.groupby("player_id")
        .agg(first_login=("event_date", "min"))
        .reset_index()
    )

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 511. Game Play Analysis I?

LeetCode 511. Game Play Analysis I is rated Easy on LeetCode.

What topics does LeetCode 511. Game Play Analysis I cover?

LeetCode 511. Game Play Analysis I is tagged Database on LeetCode.

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.