mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			144 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			144 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>表:<code>stores</code></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
+-------------+---------+
 | 
						||
| Column Name | Type    |
 | 
						||
+-------------+---------+
 | 
						||
| store_id    | int     |
 | 
						||
| store_name  | varchar |
 | 
						||
| location    | varchar |
 | 
						||
+-------------+---------+
 | 
						||
store_id 是这张表的唯一主键。
 | 
						||
每一行包含有关商店及其位置的信息。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p>表:<code>inventory</code></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
+-------------+---------+
 | 
						||
| Column Name | Type    |
 | 
						||
+-------------+---------+
 | 
						||
| inventory_id| int     |
 | 
						||
| store_id    | int     |
 | 
						||
| product_name| varchar |
 | 
						||
| quantity    | int     |
 | 
						||
| price       | decimal |
 | 
						||
+-------------+---------+
 | 
						||
inventory_id 是这张表的唯一主键。
 | 
						||
每一行代表特定商店中某一特定产品的库存情况。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p>编写一个解决方案来查找库存不平衡的商店 - 即最贵商品的库存比最便宜商品少的商店。</p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li>对于每个商店,识别 <strong>最贵的商品</strong>(最高价格)及其数量,如果有多个最贵的商品则选取数量最多的一个。</li>
 | 
						||
	<li>对于每个商店,识别 <strong>最便宜的商品</strong>(最低价格)及其数量,如果有多个最便宜的物品则选取数量最多的一个。</li>
 | 
						||
	<li>如果最贵商品的数量 <strong>少于</strong> 最便宜商品的数量,则商店存在库存不平衡。</li>
 | 
						||
	<li>按(最便宜商品的数量/最贵商品的数量)计算 <strong>不平衡比</strong>。</li>
 | 
						||
	<li>不平衡比 <strong>舍入到 2 位</strong> 小数</li>
 | 
						||
	<li>结果只包含 <strong>至少有</strong><strong> </strong><code>3</code> <strong>个不同商品</strong> 的店铺</li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p>返回结果表以不平衡比率降序排列,然后按商店名称升序排列。</p>
 | 
						||
 | 
						||
<p>结果格式如下所示。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong class="example">示例:</strong></p>
 | 
						||
 | 
						||
<div class="example-block">
 | 
						||
<p><strong>输入:</strong></p>
 | 
						||
 | 
						||
<p>stores 表:</p>
 | 
						||
 | 
						||
<pre class="example-io">
 | 
						||
+----------+----------------+-------------+
 | 
						||
| 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     |
 | 
						||
+----------+----------------+-------------+
 | 
						||
</pre>
 | 
						||
 | 
						||
<p>inventory 表:</p>
 | 
						||
 | 
						||
<pre class="example-io">
 | 
						||
+--------------+----------+--------------+----------+--------+
 | 
						||
| 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 |
 | 
						||
+--------------+----------+--------------+----------+--------+
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>输出:</strong></p>
 | 
						||
 | 
						||
<pre class="example-io">
 | 
						||
+----------+----------------+-------------+------------------+--------------------+------------------+
 | 
						||
| 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            |
 | 
						||
+----------+----------------+-------------+------------------+--------------------+------------------+
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>解释:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><strong>Downtown Tech (store_id = 1):</strong>
 | 
						||
 | 
						||
	<ul>
 | 
						||
		<li>最贵的商品:笔记本($999.99)数量为 5</li>
 | 
						||
		<li>最便宜的商品:鼠标($19.99)数量为 50</li>
 | 
						||
		<li>库存不平衡:5 < 50(贵的商品的库存更少)</li>
 | 
						||
		<li>不平衡比:50 / 5 = 10.00</li>
 | 
						||
		<li>有 4 件商品(≥ 3),所以满足要求</li>
 | 
						||
	</ul>
 | 
						||
	</li>
 | 
						||
	<li><strong>Suburb Mall (store_id = 2):</strong>
 | 
						||
	<ul>
 | 
						||
		<li>最贵的商品:手机($699.99)数量为 3</li>
 | 
						||
		<li>最便宜的商品:保护壳($15.99)数量为75</li>
 | 
						||
		<li>库存不平衡:3 < 75(贵的商品的库存更少)</li>
 | 
						||
		<li>不平衡比:75 / 3 = 25.00</li>
 | 
						||
		<li>有 4 件商品(≥ 3),所以满足要求</li>
 | 
						||
	</ul>
 | 
						||
	</li>
 | 
						||
	<li><strong>City Center (store_id = 3):</strong>
 | 
						||
	<ul>
 | 
						||
		<li>最贵的商品:平板电脑($499.99)数量为 2</li>
 | 
						||
		<li>最便宜的商品:触控笔($29.99)数量为 80</li>
 | 
						||
		<li>不平衡比:2 < 80(贵的商品的库存更少)</li>
 | 
						||
		<li>不平衡比:80 / 2 = 40.00</li>
 | 
						||
		<li>有 3 件商品(≥ 3),所以满足要求</li>
 | 
						||
	</ul>
 | 
						||
	</li>
 | 
						||
	<li><strong>未包含的商店:</strong>
 | 
						||
	<ul>
 | 
						||
		<li>Corner Shop(store_id = 4):只有两件商品(手表,手环)- 不满足最少 3 件商品的要求</li>
 | 
						||
		<li>Plaza Store(store_id = 5):只有两件商品(相机,镜头)- 不满足最少 3 件商品的要求</li>
 | 
						||
	</ul>
 | 
						||
	</li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p>结果表按不平衡比降序排序,然后以商店名升序排序。</p>
 | 
						||
</div>
 |