Game Play Analysis IV — LeetCode 550 Python Solution
MediumDatabase
- Problem
- #550
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Work out the fraction of players who logged in again on the day immediately after their very first login, rounded to two decimal places. 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 a single column named fraction.
Example
- Input
- Activity(player_id, device_id, event_date, games_played) = (1, 2, '2023-01-03', 5), (1, 2, '2023-01-04', 6), (2, 3, '2023-02-11', 1), (3, 1, '2023-01-05', 0), (3, 4, '2023-01-09', 4)
- Output
- fraction = 0.33
- Explanation
- Player 1 is the only one who logged in the day after their first login, and 1 / 3 rounds to 0.33.
Python solution
Python
import pandas as pd
def gameplay_analysis(activity: pd.DataFrame) -> pd.DataFrame:
activity["first"] = activity.groupby("player_id").event_date.transform(min)
activity_2nd_day = activity[
activity["first"] + pd.DateOffset(1) == activity["event_date"]
]
return pd.DataFrame(
{"fraction": [round(len(activity_2nd_day) / activity.player_id.nunique(), 2)]}
)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 550. Game Play Analysis IV?
- LeetCode 550. Game Play Analysis IV is rated Medium on LeetCode.
- What topics does LeetCode 550. Game Play Analysis IV cover?
- LeetCode 550. Game Play Analysis IV is tagged Database on LeetCode.