LeetCode 题解工作台
查找库存不平衡的店铺
表: stores +-------------+---------+ | Column Name | Type | +-------------+---------+ | store_id | int | | store_name | varchar | | location | varchar …
0
题型
2
代码语言
0
相关题
当前训练重点
中等 · 查找·stores·inventory·imbalance·core·interview·pattern
答案摘要
我们可以使用窗口函数来计算每个商店的最贵和最便宜商品,并且使用连接来筛选出库存不平衡的商店。具体步骤如下: 1. 计算每个商店的最贵商品:使用 `RANK()` 窗口函数按价格降序排列,并在数量相同的情况下按数量降序排列,选取排名第一的商品。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 查找·stores·inventory·imbalance·core·interview·pattern 题型思路
题目描述
表:stores
+-------------+---------+ | Column Name | Type | +-------------+---------+ | store_id | int | | store_name | varchar | | location | varchar | +-------------+---------+ store_id 是这张表的唯一主键。 每一行包含有关商店及其位置的信息。
表:inventory
+-------------+---------+ | Column Name | Type | +-------------+---------+ | inventory_id| int | | store_id | int | | product_name| varchar | | quantity | int | | price | decimal | +-------------+---------+ inventory_id 是这张表的唯一主键。 每一行代表特定商店中某一特定产品的库存情况。
编写一个解决方案来查找库存不平衡的商店 - 即最贵商品的库存比最便宜商品少的商店。
- 对于每个商店,识别 最贵的商品(最高价格)及其数量,如果有多个最贵的商品则选取数量最多的一个。
- 对于每个商店,识别 最便宜的商品(最低价格)及其数量,如果有多个最便宜的物品则选取数量最多的一个。
- 如果最贵商品的数量 少于 最便宜商品的数量,则商店存在库存不平衡。
- 按(最便宜商品的数量/最贵商品的数量)计算 不平衡比。
- 不平衡比 舍入到 2 位 小数
- 结果只包含 至少有
3个不同商品 的店铺
返回结果表以不平衡比率降序排列,然后按商店名称升序排列。
结果格式如下所示。
示例:
输入:
stores 表:
+----------+----------------+-------------+ | store_id | store_name | location | +----------+----------------+-------------+ | 1 | Downtown Tech | New York | | 2 | Suburb Mall | Chicago | | 3 | City Center | Los Angeles | | 4 | Corner Shop | Miami | | 5 | Plaza Store | Seattle | +----------+----------------+-------------+
inventory 表:
+--------------+----------+--------------+----------+--------+ | inventory_id | store_id | product_name | quantity | price | +--------------+----------+--------------+----------+--------+ | 1 | 1 | Laptop | 5 | 999.99 | | 2 | 1 | Mouse | 50 | 19.99 | | 3 | 1 | Keyboard | 25 | 79.99 | | 4 | 1 | Monitor | 15 | 299.99 | | 5 | 2 | Phone | 3 | 699.99 | | 6 | 2 | Charger | 100 | 25.99 | | 7 | 2 | Case | 75 | 15.99 | | 8 | 2 | Headphones | 20 | 149.99 | | 9 | 3 | Tablet | 2 | 499.99 | | 10 | 3 | Stylus | 80 | 29.99 | | 11 | 3 | Cover | 60 | 39.99 | | 12 | 4 | Watch | 10 | 299.99 | | 13 | 4 | Band | 25 | 49.99 | | 14 | 5 | Camera | 8 | 599.99 | | 15 | 5 | Lens | 12 | 199.99 | +--------------+----------+--------------+----------+--------+
输出:
+----------+----------------+-------------+------------------+--------------------+------------------+ | store_id | store_name | location | most_exp_product | cheapest_product | imbalance_ratio | +----------+----------------+-------------+------------------+--------------------+------------------+ | 3 | City Center | Los Angeles | Tablet | Stylus | 40.00 | | 1 | Downtown Tech | New York | Laptop | Mouse | 10.00 | | 2 | Suburb Mall | Chicago | Phone | Case | 25.00 | +----------+----------------+-------------+------------------+--------------------+------------------+
解释:
- Downtown Tech (store_id = 1):
- 最贵的商品:笔记本($999.99)数量为 5
- 最便宜的商品:鼠标($19.99)数量为 50
- 库存不平衡:5 < 50(贵的商品的库存更少)
- 不平衡比:50 / 5 = 10.00
- 有 4 件商品(≥ 3),所以满足要求
- Suburb Mall (store_id = 2):
- 最贵的商品:手机($699.99)数量为 3
- 最便宜的商品:保护壳($15.99)数量为75
- 库存不平衡:3 < 75(贵的商品的库存更少)
- 不平衡比:75 / 3 = 25.00
- 有 4 件商品(≥ 3),所以满足要求
- City Center (store_id = 3):
- 最贵的商品:平板电脑($499.99)数量为 2
- 最便宜的商品:触控笔($29.99)数量为 80
- 不平衡比:2 < 80(贵的商品的库存更少)
- 不平衡比:80 / 2 = 40.00
- 有 3 件商品(≥ 3),所以满足要求
- 未包含的商店:
- Corner Shop(store_id = 4):只有两件商品(手表,手环)- 不满足最少 3 件商品的要求
- Plaza Store(store_id = 5):只有两件商品(相机,镜头)- 不满足最少 3 件商品的要求
结果表按不平衡比降序排序,然后以商店名升序排序。
解题思路
方法一:窗口函数 + 连接
我们可以使用窗口函数来计算每个商店的最贵和最便宜商品,并且使用连接来筛选出库存不平衡的商店。具体步骤如下:
- 计算每个商店的最贵商品:使用
RANK()窗口函数按价格降序排列,并在数量相同的情况下按数量降序排列,选取排名第一的商品。 - 计算每个商店的最便宜商品:使用
RANK()窗口函数按价格升序排列,并在数量相同的情况下按数量降序排列,选取排名第一的商品。 - 筛选至少有 3 个不同商品的商店:使用
COUNT()窗口函数来统计每个商店的商品数量,并筛选出数量大于等于 3 的商店。 - 连接最贵和最便宜商品:将最贵商品和最便宜商品的结果进行连接,确保最贵商品的数量小于最便宜商品的数量。
- 计算不平衡比:计算最便宜商品数量与最贵商品数量的比率,并将其舍入到两位小数。
- 连接商店信息:将结果与商店信息表进行连接,以获取商店名称和位置。
- 排序结果:按不平衡比降序排列,然后按商店名称升序排列。
# Write your MySQL query statement below
WITH
T AS (
SELECT
store_id,
product_name,
quantity,
RANK() OVER (
PARTITION BY store_id
ORDER BY price DESC, quantity DESC
) rk1,
RANK() OVER (
PARTITION BY store_id
ORDER BY price, quantity DESC
) rk2,
COUNT(1) OVER (PARTITION BY store_id) cnt
FROM inventory
),
P1 AS (
SELECT *
FROM T
WHERE rk1 = 1 AND cnt >= 3
),
P2 AS (
SELECT *
FROM T
WHERE rk2 = 1
)
SELECT
s.store_id store_id,
store_name,
location,
p1.product_name most_exp_product,
p2.product_name cheapest_product,
ROUND(p2.quantity / p1.quantity, 2) imbalance_ratio
FROM
P1 p1
JOIN P2 p2 ON p1.store_id = p2.store_id AND p1.quantity < p2.quantity
JOIN stores s ON p1.store_id = s.store_id
ORDER BY imbalance_ratio DESC, store_name;
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
They want you to recognize a per-group boundary comparison, not a global cheapest-versus-expensive check.
- question_mark
They are testing whether you can preserve row-level quantity after aggregating price by store.
- question_mark
They expect you to notice tie handling at the minimum or maximum price before writing the final filter.
常见陷阱
外企场景- error
Comparing the minimum quantity and maximum quantity instead of the quantity attached to the minimum and maximum price.
- error
Joining min and max price summaries back to inventory without controlling duplicates when several products share the same boundary price.
- error
Forgetting that the comparison must stay inside each store, which can accidentally mix products across stores.
进阶变体
外企场景- arrow_right_alt
Return the exact cheapest and most expensive product names alongside each imbalanced store.
- arrow_right_alt
Treat a store as imbalanced only when every highest-priced tied product has lower stock than every lowest-priced tied product.
- arrow_right_alt
Rank stores by the quantity gap between the cheapest and most expensive boundary products.