|
| 1 | +Here's a well-structured `README.md` for **LeetCode 321 - Restaurant Growth**, formatted for a GitHub repository: |
| 2 | + |
| 3 | +```md |
| 4 | +# 🍽️ Restaurant Growth - LeetCode 321 |
| 5 | + |
| 6 | +## 📌 Problem Statement |
| 7 | +You are given a table **Customer**, which records daily customer transactions in a restaurant. |
| 8 | +The restaurant owner wants to analyze a **7-day moving average** of customer spending. |
| 9 | + |
| 10 | +### 📊 Customer Table |
| 11 | +| Column Name | Type | |
| 12 | +| ----------- | ------- | |
| 13 | +| customer_id | int | |
| 14 | +| name | varchar | |
| 15 | +| visited_on | date | |
| 16 | +| amount | int | |
| 17 | +- **(customer_id, visited_on) is the primary key**. |
| 18 | +- `visited_on` represents the date a customer visited the restaurant. |
| 19 | +- `amount` represents the total amount paid by a customer on that day. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 🔢 Goal: |
| 24 | +Compute the **7-day moving average** of customer spending. |
| 25 | +- The window consists of **current day + 6 days before**. |
| 26 | +- `average_amount` should be **rounded to 2 decimal places**. |
| 27 | +- The result should be **ordered by `visited_on` in ascending order**. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 📊 Example 1: |
| 32 | +### **Input:** |
| 33 | +#### **Customer Table** |
| 34 | +| customer_id | name | visited_on | amount | |
| 35 | +| ----------- | ------- | ---------- | ------ | |
| 36 | +| 1 | Jhon | 2019-01-01 | 100 | |
| 37 | +| 2 | Daniel | 2019-01-02 | 110 | |
| 38 | +| 3 | Jade | 2019-01-03 | 120 | |
| 39 | +| 4 | Khaled | 2019-01-04 | 130 | |
| 40 | +| 5 | Winston | 2019-01-05 | 110 | |
| 41 | +| 6 | Elvis | 2019-01-06 | 140 | |
| 42 | +| 7 | Anna | 2019-01-07 | 150 | |
| 43 | +| 8 | Maria | 2019-01-08 | 80 | |
| 44 | +| 9 | Jaze | 2019-01-09 | 110 | |
| 45 | +| 1 | Jhon | 2019-01-10 | 130 | |
| 46 | +| 3 | Jade | 2019-01-10 | 150 | |
| 47 | + |
| 48 | +### **Output:** |
| 49 | +| visited_on | amount | average_amount | |
| 50 | +| ---------- | ------ | -------------- | |
| 51 | +| 2019-01-07 | 860 | 122.86 | |
| 52 | +| 2019-01-08 | 840 | 120 | |
| 53 | +| 2019-01-09 | 840 | 120 | |
| 54 | +| 2019-01-10 | 1000 | 142.86 | |
| 55 | + |
| 56 | +### **Explanation:** |
| 57 | +1. **First moving average (2019-01-01 to 2019-01-07)** |
| 58 | + \[ |
| 59 | + (100 + 110 + 120 + 130 + 110 + 140 + 150) / 7 = 122.86 |
| 60 | + \] |
| 61 | +2. **Second moving average (2019-01-02 to 2019-01-08)** |
| 62 | + \[ |
| 63 | + (110 + 120 + 130 + 110 + 140 + 150 + 80) / 7 = 120 |
| 64 | + \] |
| 65 | +3. **Third moving average (2019-01-03 to 2019-01-09)** |
| 66 | + \[ |
| 67 | + (120 + 130 + 110 + 140 + 150 + 80 + 110) / 7 = 120 |
| 68 | + \] |
| 69 | +4. **Fourth moving average (2019-01-04 to 2019-01-10)** |
| 70 | + \[ |
| 71 | + (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150) / 7 = 142.86 |
| 72 | + \] |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +## 🖥 SQL Solutions |
| 77 | + |
| 78 | +### 1️⃣ **Using `WINDOW FUNCTION` (`SUM() OVER` + `RANK() OVER`)** |
| 79 | +#### **Explanation:** |
| 80 | +- First, **group transactions per day** using `SUM(amount)`. |
| 81 | +- Then, use `SUM() OVER (ROWS 6 PRECEDING)` to calculate **moving sum** over 7 days. |
| 82 | +- Use `RANK()` to track row numbers and filter rows with `rk > 6`. |
| 83 | +- Finally, compute `ROUND(amount / 7, 2)`. |
| 84 | + |
| 85 | +```sql |
| 86 | +WITH t AS ( |
| 87 | + SELECT |
| 88 | + visited_on, |
| 89 | + SUM(amount) OVER ( |
| 90 | + ORDER BY visited_on |
| 91 | + ROWS 6 PRECEDING |
| 92 | + ) AS amount, |
| 93 | + RANK() OVER ( |
| 94 | + ORDER BY visited_on |
| 95 | + ROWS 6 PRECEDING |
| 96 | + ) AS rk |
| 97 | + FROM ( |
| 98 | + SELECT visited_on, SUM(amount) AS amount |
| 99 | + FROM Customer |
| 100 | + GROUP BY visited_on |
| 101 | + ) AS tt |
| 102 | +) |
| 103 | +SELECT visited_on, amount, ROUND(amount / 7, 2) AS average_amount |
| 104 | +FROM t |
| 105 | +WHERE rk > 6; |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +### 2️⃣ **Using `JOIN` + `DATEDIFF()`** |
| 111 | +#### **Explanation:** |
| 112 | +- Use a **self-join** to find transactions **within a 7-day range**. |
| 113 | +- Sum the `amount` for each window and calculate the moving average. |
| 114 | +- Use `DATEDIFF(a.visited_on, b.visited_on) BETWEEN 0 AND 6` to filter records. |
| 115 | +- Ensure only complete 7-day windows are included. |
| 116 | + |
| 117 | +```sql |
| 118 | +SELECT |
| 119 | + a.visited_on, |
| 120 | + SUM(b.amount) AS amount, |
| 121 | + ROUND(SUM(b.amount) / 7, 2) AS average_amount |
| 122 | +FROM |
| 123 | + (SELECT DISTINCT visited_on FROM customer) AS a |
| 124 | + JOIN customer AS b |
| 125 | + ON DATEDIFF(a.visited_on, b.visited_on) BETWEEN 0 AND 6 |
| 126 | +WHERE |
| 127 | + a.visited_on >= (SELECT MIN(visited_on) FROM customer) + 6 |
| 128 | +GROUP BY a.visited_on |
| 129 | +ORDER BY a.visited_on; |
| 130 | +``` |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +## 🐍 Pandas Solution (Python) |
| 135 | +#### **Explanation:** |
| 136 | +- **Group by `visited_on`** and sum `amount` per day. |
| 137 | +- **Use `.rolling(7).sum()`** to compute the moving sum over 7 days. |
| 138 | +- **Drop NaN values** to exclude incomplete windows. |
| 139 | +- **Round the average to 2 decimal places**. |
| 140 | + |
| 141 | +```python |
| 142 | +import pandas as pd |
| 143 | + |
| 144 | +def restaurant_growth(customers: pd.DataFrame) -> pd.DataFrame: |
| 145 | + # Aggregate daily amounts |
| 146 | + daily_amount = customers.groupby("visited_on")["amount"].sum().reset_index() |
| 147 | + |
| 148 | + # Compute rolling 7-day sum and moving average |
| 149 | + daily_amount["amount"] = daily_amount["amount"].rolling(7).sum() |
| 150 | + daily_amount["average_amount"] = (daily_amount["amount"] / 7).round(2) |
| 151 | + |
| 152 | + # Drop incomplete windows |
| 153 | + daily_amount = daily_amount.dropna().reset_index(drop=True) |
| 154 | + |
| 155 | + return daily_amount |
| 156 | +``` |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## 📁 File Structure |
| 161 | +``` |
| 162 | +📂 Restaurant-Growth |
| 163 | +│── 📜 README.md |
| 164 | +│── 📜 solution.sql |
| 165 | +│── 📜 solution_pandas.py |
| 166 | +│── 📜 test_cases.sql |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## 🔗 Useful Links |
| 172 | +- 📖 [LeetCode Problem](https://leetcode.com/problems/restaurant-growth/) |
| 173 | +- 📚 [SQL `WINDOW FUNCTIONS` Documentation](https://www.w3schools.com/sql/sql_window.asp) |
| 174 | +- 🐍 [Pandas Rolling Window](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html) |
| 175 | +``` |
| 176 | +
|
| 177 | +### Features of this `README.md`: |
| 178 | +✅ **Clear problem statement with table structure** |
| 179 | +✅ **Examples with detailed calculations** |
| 180 | +✅ **SQL and Pandas solutions with explanations** |
| 181 | +✅ **Alternative SQL query for flexibility** |
| 182 | +✅ **File structure for GitHub organization** |
| 183 | +✅ **Useful reference links** |
| 184 | +
|
| 185 | +Would you like any refinements? 🚀 |
0 commit comments