|
| 1 | +Here's the **README.md** file including **MySQL, Window SQL, and Pandas solutions** for **LeetCode 1164 - Product Price at a Given Date**. |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# **1164. Product Price at a Given Date** |
| 6 | + |
| 7 | +## **Problem Statement** |
| 8 | +You are given the **Products** table, which keeps track of price changes. |
| 9 | + |
| 10 | +### **Products Table** |
| 11 | +``` |
| 12 | ++------------+-----------+-------------+ |
| 13 | +| product_id | new_price | change_date | |
| 14 | ++------------+-----------+-------------+ |
| 15 | +| int | int | date | |
| 16 | ++------------+-----------+-------------+ |
| 17 | +``` |
| 18 | +- `(product_id, change_date)` is the **primary key**. |
| 19 | +- Each row represents a price update for a product on a specific date. |
| 20 | + |
| 21 | +### **Task:** |
| 22 | +Find the price of all products on **2019年08月16日**. |
| 23 | +Assume the **initial price of all products is 10** before any change occurs. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## **Example 1:** |
| 28 | + |
| 29 | +### **Input:** |
| 30 | +**Products Table** |
| 31 | +``` |
| 32 | ++------------+-----------+-------------+ |
| 33 | +| product_id | new_price | change_date | |
| 34 | ++------------+-----------+-------------+ |
| 35 | +| 1 | 20 | 2019年08月14日 | |
| 36 | +| 2 | 50 | 2019年08月14日 | |
| 37 | +| 1 | 30 | 2019年08月15日 | |
| 38 | +| 1 | 35 | 2019年08月16日 | |
| 39 | +| 2 | 65 | 2019年08月17日 | |
| 40 | +| 3 | 20 | 2019年08月18日 | |
| 41 | ++------------+-----------+-------------+ |
| 42 | +``` |
| 43 | + |
| 44 | +### **Output:** |
| 45 | +``` |
| 46 | ++------------+-------+ |
| 47 | +| product_id | price | |
| 48 | ++------------+-------+ |
| 49 | +| 2 | 50 | |
| 50 | +| 1 | 35 | |
| 51 | +| 3 | 10 | |
| 52 | ++------------+-------+ |
| 53 | +``` |
| 54 | + |
| 55 | +### **Explanation:** |
| 56 | +- **Product 1:** Last change before `2019年08月16日` → **35** |
| 57 | +- **Product 2:** Last change before `2019年08月16日` → **50** |
| 58 | +- **Product 3:** **No price change before 2019年08月16日**, so default price is **10** |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## **SQL Solutions** |
| 63 | + |
| 64 | +### **1️⃣ Standard MySQL Solution** |
| 65 | +```sql |
| 66 | +SELECT |
| 67 | + p.product_id, |
| 68 | + COALESCE(( |
| 69 | + SELECT new_price |
| 70 | + FROM Products |
| 71 | + WHERE product_id = p.product_id |
| 72 | + AND change_date <= '2019年08月16日' |
| 73 | + ORDER BY change_date DESC |
| 74 | + LIMIT 1 |
| 75 | + ), 10) AS price |
| 76 | +FROM |
| 77 | + (SELECT DISTINCT product_id FROM Products) p; |
| 78 | +``` |
| 79 | +#### **Explanation:** |
| 80 | +1. **Find the last price before or on `2019年08月16日`** |
| 81 | + - `ORDER BY change_date DESC LIMIT 1` → Gets the most recent price before `2019年08月16日`. |
| 82 | +2. **Use `COALESCE()`** |
| 83 | + - If no price exists, set default price **10**. |
| 84 | +3. **Use `DISTINCT product_id`** |
| 85 | + - Ensures all unique products are checked. |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +### **2️⃣ Window Function (SQL) Solution** |
| 90 | +```sql |
| 91 | +WITH RankedPrices AS ( |
| 92 | + SELECT |
| 93 | + product_id, |
| 94 | + new_price AS price, |
| 95 | + change_date, |
| 96 | + RANK() OVER (PARTITION BY product_id ORDER BY change_date DESC) AS rnk |
| 97 | + FROM Products |
| 98 | + WHERE change_date <= '2019年08月16日' |
| 99 | +) |
| 100 | +SELECT p.product_id, COALESCE(rp.price, 10) AS price |
| 101 | +FROM (SELECT DISTINCT product_id FROM Products) p |
| 102 | +LEFT JOIN RankedPrices rp ON p.product_id = rp.product_id AND rp.rnk = 1; |
| 103 | +``` |
| 104 | +#### **Explanation:** |
| 105 | +1. **`RANK() OVER (PARTITION BY product_id ORDER BY change_date DESC)`** |
| 106 | + - Assigns **rank 1** to the last price before `2019年08月16日`. |
| 107 | +2. **`LEFT JOIN` with `DISTINCT product_id`** |
| 108 | + - Ensures all products are included. |
| 109 | +3. **Use `COALESCE(price, 10)`** |
| 110 | + - If no price exists, set default **10**. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## **Pandas Solution (Python)** |
| 115 | +```python |
| 116 | +import pandas as pd |
| 117 | + |
| 118 | +# Sample Data |
| 119 | +products_data = { |
| 120 | + 'product_id': [1, 2, 1, 1, 2, 3], |
| 121 | + 'new_price': [20, 50, 30, 35, 65, 20], |
| 122 | + 'change_date': ['2019年08月14日', '2019年08月14日', '2019年08月15日', '2019年08月16日', '2019年08月17日', '2019年08月18日'] |
| 123 | +} |
| 124 | + |
| 125 | +# Create DataFrame |
| 126 | +products_df = pd.DataFrame(products_data) |
| 127 | +products_df['change_date'] = pd.to_datetime(products_df['change_date']) # Convert to datetime |
| 128 | + |
| 129 | +# Filter for changes before or on '2019年08月16日' |
| 130 | +valid_prices = products_df[products_df['change_date'] <= '2019年08月16日'] |
| 131 | + |
| 132 | +# Get the latest price for each product before '2019年08月16日' |
| 133 | +latest_prices = valid_prices.sort_values(by=['product_id', 'change_date']).groupby('product_id').last().reset_index() |
| 134 | + |
| 135 | +# Rename column |
| 136 | +latest_prices = latest_prices[['product_id', 'new_price']].rename(columns={'new_price': 'price'}) |
| 137 | + |
| 138 | +# Get all unique products |
| 139 | +all_products = products_df[['product_id']].drop_duplicates() |
| 140 | + |
| 141 | +# Merge with latest prices and fill missing values with 10 |
| 142 | +final_prices = all_products.merge(latest_prices, on='product_id', how='left').fillna({'price': 10}) |
| 143 | + |
| 144 | +print(final_prices) |
| 145 | +``` |
| 146 | + |
| 147 | +### **Explanation:** |
| 148 | +1. **Convert `change_date` to datetime** |
| 149 | + - Ensures proper date comparison. |
| 150 | +2. **Filter for prices before `2019年08月16日`** |
| 151 | + - Excludes future price changes. |
| 152 | +3. **Get the latest price per product (`groupby().last()`)** |
| 153 | + - Retrieves the most recent price change. |
| 154 | +4. **Merge with all products and set missing prices to `10`** |
| 155 | + - Ensures all products are included. |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +## **File Structure** |
| 160 | +``` |
| 161 | +LeetCode1164/ |
| 162 | +├── problem_statement.md # Contains the problem description and constraints. |
| 163 | +├── sql_solution.sql # Contains the SQL solutions (Standard + Window Functions). |
| 164 | +├── pandas_solution.py # Contains the Pandas solution. |
| 165 | +├── README.md # Overview of the problem and available solutions. |
| 166 | +``` |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## **Useful Links** |
| 171 | +- [LeetCode Problem 1164](https://leetcode.com/problems/product-price-at-a-given-date/) |
| 172 | +- [SQL COALESCE Documentation](https://www.w3schools.com/sql/sql_coalesce.asp) |
| 173 | +- [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html) |
| 174 | + |
0 commit comments