Skip to content

Commit b7ea00a

Browse files
committed
Update readme.md
1 parent e3c9081 commit b7ea00a

File tree

1 file changed

+172
-0
lines changed
  • LeetCode SQL 50 Solution/550. Game Play Analysis IV

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
Here’s the structured format for **LeetCode 550: Game Play Analysis IV**:
2+
3+
---
4+
5+
# **550. Game Play Analysis IV**
6+
7+
## **Problem Statement**
8+
You are given a table named `Activity`, which logs the gaming activity of players.
9+
10+
### **Activity Table**
11+
```
12+
+--------------+---------+
13+
| Column Name | Type |
14+
+--------------+---------+
15+
| player_id | int |
16+
| device_id | int |
17+
| event_date | date |
18+
| games_played | int |
19+
+--------------+---------+
20+
```
21+
- **(player_id, event_date)** is the **primary key**.
22+
- Each row contains:
23+
- `player_id`: The ID of the player.
24+
- `event_date`: The date when the player logged in.
25+
- `games_played`: The number of games played before logging out.
26+
27+
### **Task:**
28+
Find the **fraction** of players who logged in **again** the day after their **first login date**, rounded to **2 decimal places**.
29+
30+
---
31+
32+
## **Example 1:**
33+
### **Input:**
34+
#### **Activity Table**
35+
```
36+
+-----------+-----------+------------+--------------+
37+
| player_id | device_id | event_date | games_played |
38+
+-----------+-----------+------------+--------------+
39+
| 1 | 2 | 2016-03-01 | 5 |
40+
| 1 | 2 | 2016-03-02 | 6 |
41+
| 2 | 3 | 2017-06-25 | 1 |
42+
| 3 | 1 | 2016-03-02 | 0 |
43+
| 3 | 4 | 2018-07-03 | 5 |
44+
+-----------+-----------+------------+--------------+
45+
```
46+
### **Output:**
47+
```
48+
+-----------+
49+
| fraction |
50+
+-----------+
51+
| 0.33 |
52+
+-----------+
53+
```
54+
### **Explanation:**
55+
- `player_id = 1`: First login on **2016-03-01**, logs in again on **2016-03-02**
56+
- `player_id = 2`: First login on **2017-06-25**, no next-day login ❌
57+
- `player_id = 3`: First login on **2016-03-02**, no next-day login ❌
58+
59+
Total players = **3**, Players who logged in the next day = **1****1 / 3 = 0.33**
60+
61+
---
62+
63+
## **Solution Approaches**
64+
65+
### **SQL Solution (Using `JOIN` & `DATEDIFF`)**
66+
```sql
67+
SELECT
68+
ROUND((
69+
SELECT COUNT(DISTINCT a.player_id)
70+
FROM Activity a
71+
INNER JOIN (
72+
SELECT player_id, MIN(event_date) AS first_login
73+
FROM Activity
74+
GROUP BY player_id
75+
) b
76+
ON a.player_id = b.player_id
77+
AND DATEDIFF(a.event_date, b.first_login) = 1
78+
) /
79+
(SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction;
80+
```
81+
**Explanation:**
82+
1. **Find First Login Date per Player**
83+
- `MIN(event_date) AS first_login`
84+
- **Grouped by** `player_id`
85+
2. **Find Players Who Logged in on the Next Day**
86+
- **Join** the table with itself.
87+
- Use `DATEDIFF(a.event_date, b.first_login) = 1` to check next-day logins.
88+
- Count unique `player_id`s.
89+
3. **Calculate Fraction**
90+
- Divide by total distinct `player_id`s.
91+
- Round to **2 decimal places**.
92+
93+
---
94+
95+
### **Alternative SQL Solution (Using `EXISTS`)**
96+
```sql
97+
SELECT ROUND(
98+
(SELECT COUNT(DISTINCT player_id)
99+
FROM Activity a
100+
WHERE EXISTS (
101+
SELECT 1 FROM Activity b
102+
WHERE a.player_id = b.player_id
103+
AND DATEDIFF(b.event_date, a.event_date) = 1
104+
)) /
105+
(SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction;
106+
```
107+
**Explanation:**
108+
- Checks if a player has **ANY** login exactly **one day after**.
109+
- Uses `EXISTS` to optimize performance.
110+
111+
---
112+
113+
### **Pandas Solution**
114+
```python
115+
import pandas as pd
116+
117+
def game_play_analysis(activity: pd.DataFrame) -> pd.DataFrame:
118+
# Get first login date for each player
119+
first_login = activity.groupby("player_id")["event_date"].min().reset_index()
120+
first_login.columns = ["player_id", "first_login"]
121+
122+
# Merge first login date with original table
123+
merged = activity.merge(first_login, on="player_id")
124+
125+
# Filter players who logged in the next day
126+
next_day_logins = merged[
127+
(merged["event_date"] - merged["first_login"]).dt.days == 1
128+
]["player_id"].nunique()
129+
130+
# Total unique players
131+
total_players = activity["player_id"].nunique()
132+
133+
# Calculate fraction
134+
fraction = round(next_day_logins / total_players, 2)
135+
136+
return pd.DataFrame({"fraction": [fraction]})
137+
```
138+
**Explanation:**
139+
1. **Find First Login Date**
140+
- Group by `player_id`, get `min(event_date)`.
141+
2. **Merge with Original Table**
142+
- Check if `event_date - first_login = 1 day`.
143+
3. **Count Unique Players**
144+
- Divide by total unique `player_id`s.
145+
146+
---
147+
148+
## **File Structure**
149+
```
150+
📂 LeetCode550
151+
│── 📜 problem_statement.md
152+
│── 📜 sql_solution.sql
153+
│── 📜 sql_exists_solution.sql
154+
│── 📜 pandas_solution.py
155+
│── 📜 README.md
156+
```
157+
- `problem_statement.md` → Contains the problem description.
158+
- `sql_solution.sql` → SQL solution using **JOIN & DATEDIFF**.
159+
- `sql_exists_solution.sql` → SQL solution using **EXISTS**.
160+
- `pandas_solution.py` → Pandas solution.
161+
- `README.md` → Overview of problem and solutions.
162+
163+
---
164+
165+
## **Useful Links**
166+
- [LeetCode Problem 550](https://leetcode.com/problems/game-play-analysis-iv/)
167+
- [SQL `DATEDIFF()`](https://www.w3schools.com/sql/func_mysql_datediff.asp)
168+
- [Pandas `.groupby()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
169+
170+
---
171+
172+
This structured format provides **clear problem understanding and efficient solutions**. 🚀

0 commit comments

Comments
 (0)