MediumNo. 0550
Game Play Analysis IV — LeetCode 550 Python Solution
- Topics
- Database
- 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
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
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
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.