1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-04 15:01:40 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/originData/find-stores-with-inventory-imbalance.json
2025-08-10 21:35:14 +08:00

102 lines
22 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"data": {
"question": {
"questionId": "3971",
"questionFrontendId": "3626",
"categoryTitle": "Database",
"boundTopicId": 3732905,
"title": "Find Stores with Inventory Imbalance",
"titleSlug": "find-stores-with-inventory-imbalance",
"content": "<p>Table: <code>stores</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| store_id | int |\n| store_name | varchar |\n| location | varchar |\n+-------------+---------+\nstore_id is the unique identifier for this table.\nEach row contains information about a store and its location.\n</pre>\n\n<p>Table: <code>inventory</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| inventory_id| int |\n| store_id | int |\n| product_name| varchar |\n| quantity | int |\n| price | decimal |\n+-------------+---------+\ninventory_id is the unique identifier for this table.\nEach row represents the inventory of a specific product at a specific store.\n</pre>\n\n<p>Write a solution to find stores that have <strong>inventory imbalance</strong> - stores where the most expensive product has lower stock than the cheapest product.</p>\n\n<ul>\n\t<li>For each store, identify the <strong>most expensive product</strong> (highest price) and its quantity</li>\n\t<li>For each store, identify the <strong>cheapest product</strong> (lowest price) and its quantity</li>\n\t<li>A store has inventory imbalance if the most expensive product&#39;s quantity is <strong>less than</strong> the cheapest product&#39;s quantity</li>\n\t<li>Calculate the <strong>imbalance ratio</strong> as (cheapest_quantity / most_expensive_quantity)</li>\n\t<li><strong>Round</strong> the imbalance ratio to <strong>2</strong> decimal places</li>\n\t<li>Only include stores that have <strong>at least </strong><code>3</code><strong> different products</strong></li>\n</ul>\n\n<p>Return <em>the result table ordered by imbalance ratio in <strong>descending</strong> order, then by store name in <strong>ascending</strong> order</em>.</p>\n\n<p>The result format is in the following example.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong></p>\n\n<p>stores table:</p>\n\n<pre class=\"example-io\">\n+----------+----------------+-------------+\n| store_id | store_name | location |\n+----------+----------------+-------------+\n| 1 | Downtown Tech | New York |\n| 2 | Suburb Mall | Chicago |\n| 3 | City Center | Los Angeles |\n| 4 | Corner Shop | Miami |\n| 5 | Plaza Store | Seattle |\n+----------+----------------+-------------+\n</pre>\n\n<p>inventory table:</p>\n\n<pre class=\"example-io\">\n+--------------+----------+--------------+----------+--------+\n| inventory_id | store_id | product_name | quantity | price |\n+--------------+----------+--------------+----------+--------+\n| 1 | 1 | Laptop | 5 | 999.99 |\n| 2 | 1 | Mouse | 50 | 19.99 |\n| 3 | 1 | Keyboard | 25 | 79.99 |\n| 4 | 1 | Monitor | 15 | 299.99 |\n| 5 | 2 | Phone | 3 | 699.99 |\n| 6 | 2 | Charger | 100 | 25.99 |\n| 7 | 2 | Case | 75 | 15.99 |\n| 8 | 2 | Headphones | 20 | 149.99 |\n| 9 | 3 | Tablet | 2 | 499.99 |\n| 10 | 3 | Stylus | 80 | 29.99 |\n| 11 | 3 | Cover | 60 | 39.99 |\n| 12 | 4 | Watch | 10 | 299.99 |\n| 13 | 4 | Band | 25 | 49.99 |\n| 14 | 5 | Camera | 8 | 599.99 |\n| 15 | 5 | Lens | 12 | 199.99 |\n+--------------+----------+--------------+----------+--------+\n</pre>\n\n<p><strong>Output:</strong></p>\n\n<pre class=\"example-io\">\n+----------+----------------+-------------+------------------+--------------------+------------------+\n| store_id | store_name | location | most_exp_product | cheapest_product | imbalance_ratio |\n+----------+----------------+-------------+------------------+--------------------+------------------+\n| 3 | City Center | Los Angeles | Tablet | Stylus | 40.00 |\n| 1 | Downtown Tech | New York | Laptop | Mouse | 10.00 |\n| 2 | Suburb Mall | Chicago | Phone | Case | 25.00 |\n+----------+----------------+-------------+------------------+--------------------+------------------+\n</pre>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n\t<li><strong>Downtown Tech (store_id = 1):</strong>\n\n\t<ul>\n\t\t<li>Most expensive product: Laptop ($999.99) with quantity 5</li>\n\t\t<li>Cheapest product: Mouse ($19.99) with quantity 50</li>\n\t\t<li>Inventory imbalance: 5 &lt; 50 (expensive product has lower stock)</li>\n\t\t<li>Imbalance ratio: 50 / 5 = 10.00</li>\n\t\t<li>Has 4 products (&ge; 3), so qualifies</li>\n\t</ul>\n\t</li>\n\t<li><strong>Suburb Mall (store_id = 2):</strong>\n\t<ul>\n\t\t<li>Most expensive product: Phone ($699.99) with quantity 3</li>\n\t\t<li>Cheapest product: Case ($15.99) with quantity 75</li>\n\t\t<li>Inventory imbalance: 3 &lt; 75 (expensive product has lower stock)</li>\n\t\t<li>Imbalance ratio: 75 / 3 = 25.00</li>\n\t\t<li>Has 4 products (&ge; 3), so qualifies</li>\n\t</ul>\n\t</li>\n\t<li><strong>City Center (store_id = 3):</strong>\n\t<ul>\n\t\t<li>Most expensive product: Tablet ($499.99) with quantity 2</li>\n\t\t<li>Cheapest product: Stylus ($29.99) with quantity 80</li>\n\t\t<li>Inventory imbalance: 2 &lt; 80 (expensive product has lower stock)</li>\n\t\t<li>Imbalance ratio: 80 / 2 = 40.00</li>\n\t\t<li>Has 3 products (&ge; 3), so qualifies</li>\n\t</ul>\n\t</li>\n\t<li><strong>Stores not included:</strong>\n\t<ul>\n\t\t<li>Corner Shop (store_id = 4): Only has 2 products (Watch, Band) - doesn&#39;t meet minimum 3 products requirement</li>\n\t\t<li>Plaza Store (store_id = 5): Only has 2 products (Camera, Lens) - doesn&#39;t meet minimum 3 products requirement</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>The Results table is ordered by imbalance ratio in descending order, then by store name in ascending order</p>\n</div>\n",
"translatedTitle": "查找库存不平衡的店铺",
"translatedContent": "<p>表:<code>stores</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| store_id | int |\n| store_name | varchar |\n| location | varchar |\n+-------------+---------+\nstore_id 是这张表的唯一主键。\n每一行包含有关商店及其位置的信息。\n</pre>\n\n<p>表:<code>inventory</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| inventory_id| int |\n| store_id | int |\n| product_name| varchar |\n| quantity | int |\n| price | decimal |\n+-------------+---------+\ninventory_id 是这张表的唯一主键。\n每一行代表特定商店中某一特定产品的库存情况。\n</pre>\n\n<p>编写一个解决方案来查找库存不平衡的商店 - 即最贵商品的库存比最便宜商品少的商店。</p>\n\n<ul>\n\t<li>对于每个商店,识别 <strong>最贵的商品</strong>(最高价格)及其数量,如果有多个最贵的商品则选取数量最多的一个。</li>\n\t<li>对于每个商店,识别 <strong>最便宜的商品</strong>(最低价格)及其数量,如果有多个最便宜的物品则选取数量最多的一个。</li>\n\t<li>如果最贵商品的数量 <strong>少于</strong> 最便宜商品的数量,则商店存在库存不平衡。</li>\n\t<li>按(最便宜商品的数量/最贵商品的数量)计算 <strong>不平衡比</strong>。</li>\n\t<li>不平衡比&nbsp;<strong>舍入到 2 位</strong>&nbsp;小数</li>\n\t<li>结果只包含&nbsp;<strong>至少有</strong><strong> </strong><code>3</code>&nbsp;<strong>个不同商品</strong> 的店铺</li>\n</ul>\n\n<p>返回结果表以不平衡比率降序排列,然后按商店名称升序排列。</p>\n\n<p>结果格式如下所示。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>输入:</strong></p>\n\n<p>stores 表:</p>\n\n<pre class=\"example-io\">\n+----------+----------------+-------------+\n| store_id | store_name | location |\n+----------+----------------+-------------+\n| 1 | Downtown Tech | New York |\n| 2 | Suburb Mall | Chicago |\n| 3 | City Center | Los Angeles |\n| 4 | Corner Shop | Miami |\n| 5 | Plaza Store | Seattle |\n+----------+----------------+-------------+\n</pre>\n\n<p>inventory 表:</p>\n\n<pre class=\"example-io\">\n+--------------+----------+--------------+----------+--------+\n| inventory_id | store_id | product_name | quantity | price |\n+--------------+----------+--------------+----------+--------+\n| 1 | 1 | Laptop | 5 | 999.99 |\n| 2 | 1 | Mouse | 50 | 19.99 |\n| 3 | 1 | Keyboard | 25 | 79.99 |\n| 4 | 1 | Monitor | 15 | 299.99 |\n| 5 | 2 | Phone | 3 | 699.99 |\n| 6 | 2 | Charger | 100 | 25.99 |\n| 7 | 2 | Case | 75 | 15.99 |\n| 8 | 2 | Headphones | 20 | 149.99 |\n| 9 | 3 | Tablet | 2 | 499.99 |\n| 10 | 3 | Stylus | 80 | 29.99 |\n| 11 | 3 | Cover | 60 | 39.99 |\n| 12 | 4 | Watch | 10 | 299.99 |\n| 13 | 4 | Band | 25 | 49.99 |\n| 14 | 5 | Camera | 8 | 599.99 |\n| 15 | 5 | Lens | 12 | 199.99 |\n+--------------+----------+--------------+----------+--------+\n</pre>\n\n<p><strong>输出:</strong></p>\n\n<pre class=\"example-io\">\n+----------+----------------+-------------+------------------+--------------------+------------------+\n| store_id | store_name | location | most_exp_product | cheapest_product | imbalance_ratio |\n+----------+----------------+-------------+------------------+--------------------+------------------+\n| 3 | City Center | Los Angeles | Tablet | Stylus | 40.00 |\n| 1 | Downtown Tech | New York | Laptop | Mouse | 10.00 |\n| 2 | Suburb Mall | Chicago | Phone | Case | 25.00 |\n+----------+----------------+-------------+------------------+--------------------+------------------+\n</pre>\n\n<p><strong>解释:</strong></p>\n\n<ul>\n\t<li><strong>Downtown Tech (store_id = 1)</strong>\n\n\t<ul>\n\t\t<li>最贵的商品:笔记本($999.99)数量为 5</li>\n\t\t<li>最便宜的商品:鼠标($19.99)数量为 50</li>\n\t\t<li>库存不平衡5 &lt; 50贵的商品的库存更少</li>\n\t\t<li>不平衡比50 / 5 = 10.00</li>\n\t\t<li>有 4&nbsp;件商品(≥ 3所以满足要求</li>\n\t</ul>\n\t</li>\n\t<li><strong>Suburb Mall (store_id = 2)</strong>\n\t<ul>\n\t\t<li>最贵的商品:手机($699.99)数量为 3</li>\n\t\t<li>最便宜的商品:保护壳($15.99数量为75</li>\n\t\t<li>库存不平衡3 &lt; 75贵的商品的库存更少</li>\n\t\t<li>不平衡比75 / 3 = 25.00</li>\n\t\t<li>有 4&nbsp;件商品(≥ 3所以满足要求</li>\n\t</ul>\n\t</li>\n\t<li><strong>City Center (store_id = 3)</strong>\n\t<ul>\n\t\t<li>最贵的商品:平板电脑($499.99)数量为 2</li>\n\t\t<li>最便宜的商品:触控笔($29.99)数量为 80</li>\n\t\t<li>不平衡比2 &lt; 80贵的商品的库存更少</li>\n\t\t<li>不平衡比80 / 2 = 40.00</li>\n\t\t<li>有 3 件商品(≥ 3所以满足要求</li>\n\t</ul>\n\t</li>\n\t<li><strong>未包含的商店:</strong>\n\t<ul>\n\t\t<li>Corner Shopstore_id = 4只有两件商品手表手环- 不满足最少 3 件商品的要求</li>\n\t\t<li>Plaza Storestore_id = 5只有两件商品相机镜头- 不满足最少 3 件商品的要求</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>结果表按不平衡比降序排序,然后以商店名升序排序。</p>\n</div>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 1,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "MySQL",
"langSlug": "mysql",
"code": "# Write your MySQL query statement below",
"__typename": "CodeSnippetNode"
},
{
"lang": "MS SQL Server",
"langSlug": "mssql",
"code": "/* Write your T-SQL query statement below */",
"__typename": "CodeSnippetNode"
},
{
"lang": "Oracle",
"langSlug": "oraclesql",
"code": "/* Write your PL/SQL query statement below */",
"__typename": "CodeSnippetNode"
},
{
"lang": "Pandas",
"langSlug": "pythondata",
"code": "import pandas as pd\n\ndef find_inventory_imbalance(stores: pd.DataFrame, inventory: pd.DataFrame) -> pd.DataFrame:\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "PostgreSQL",
"langSlug": "postgresql",
"code": "-- Write your PostgreSQL query statement below",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"238\", \"totalSubmission\": \"410\", \"totalAcceptedRaw\": 238, \"totalSubmissionRaw\": 410, \"acRate\": \"58.0%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "{\"headers\":{\"stores\":[\"store_id\",\"store_name\",\"location\"],\"inventory\":[\"inventory_id\",\"store_id\",\"product_name\",\"quantity\",\"price\"]},\"rows\":{\"stores\":[[1,\"Downtown Tech\",\"New York\"],[2,\"Suburb Mall\",\"Chicago\"],[3,\"City Center\",\"Los Angeles\"],[4,\"Corner Shop\",\"Miami\"],[5,\"Plaza Store\",\"Seattle\"]],\"inventory\":[[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]]}}",
"metaData": "{\"mysql\":[\"CREATE TABLE if not exists stores (\\n store_id INT,\\n store_name VARCHAR(255),\\n location VARCHAR(255)\\n)\",\"CREATE TABLE if not exists inventory (\\n inventory_id INT,\\n store_id INT,\\n product_name VARCHAR(255),\\n quantity INT,\\n price DECIMAL(10, 2)\\n)\"],\"mssql\":[\"CREATE TABLE stores (\\n store_id INT,\\n store_name VARCHAR(255),\\n location VARCHAR(255)\\n)\",\"CREATE TABLE inventory (\\n inventory_id INT,\\n store_id INT,\\n product_name VARCHAR(255),\\n quantity INT,\\n price DECIMAL(10, 2)\\n)\"],\"oraclesql\":[\"CREATE TABLE stores (\\n store_id NUMBER,\\n store_name VARCHAR2(255),\\n location VARCHAR2(255)\\n)\",\"CREATE TABLE inventory (\\n inventory_id NUMBER,\\n store_id NUMBER,\\n product_name VARCHAR2(255),\\n quantity NUMBER,\\n price NUMBER(10, 2)\\n)\"],\"database\":true,\"name\":\"find_inventory_imbalance\",\"postgresql\":[\"CREATE TABLE IF NOT EXISTS stores (\\n store_id INTEGER,\\n store_name VARCHAR(255),\\n location VARCHAR(255)\\n);\\n\",\"CREATE TABLE IF NOT EXISTS inventory (\\n inventory_id INTEGER,\\n store_id INTEGER,\\n product_name VARCHAR(255),\\n quantity INTEGER,\\n price DECIMAL(10, 2)\\n);\\n\"],\"pythondata\":[\"stores = pd.DataFrame([], columns=['store_id', 'store_name', 'location']).astype({'store_id': 'int64', 'store_name': 'string', 'location': 'string'})\\n\",\"inventory = pd.DataFrame([], columns=['inventory_id', 'store_id', 'product_name', 'quantity', 'price']).astype({'inventory_id': 'int64', 'store_id': 'int64', 'product_name': 'string', 'quantity': 'int64', 'price': 'float64'})\\n\"],\"database_schema\":{\"stores\":{\"store_id\":\"INT\",\"store_name\":\"VARCHAR(255)\",\"location\":\"VARCHAR(255)\"},\"inventory\":{\"inventory_id\":\"INT\",\"store_id\":\"INT\",\"product_name\":\"VARCHAR(255)\",\"quantity\":\"INT\",\"price\":\"DECIMAL(10, 2)\"}}}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [
"CREATE TABLE if not exists stores (\n store_id INT,\n store_name VARCHAR(255),\n location VARCHAR(255)\n)",
"CREATE TABLE if not exists inventory (\n inventory_id INT,\n store_id INT,\n product_name VARCHAR(255),\n quantity INT,\n price DECIMAL(10, 2)\n)",
"Truncate table stores",
"insert into stores (store_id, store_name, location) values ('1', 'Downtown Tech', 'New York')",
"insert into stores (store_id, store_name, location) values ('2', 'Suburb Mall', 'Chicago')",
"insert into stores (store_id, store_name, location) values ('3', 'City Center', 'Los Angeles')",
"insert into stores (store_id, store_name, location) values ('4', 'Corner Shop', 'Miami')",
"insert into stores (store_id, store_name, location) values ('5', 'Plaza Store', 'Seattle')",
"Truncate table inventory",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('1', '1', 'Laptop', '5', '999.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('2', '1', 'Mouse', '50', '19.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('3', '1', 'Keyboard', '25', '79.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('4', '1', 'Monitor', '15', '299.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('5', '2', 'Phone', '3', '699.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('6', '2', 'Charger', '100', '25.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('7', '2', 'Case', '75', '15.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('8', '2', 'Headphones', '20', '149.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('9', '3', 'Tablet', '2', '499.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('10', '3', 'Stylus', '80', '29.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('11', '3', 'Cover', '60', '39.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('12', '4', 'Watch', '10', '299.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('13', '4', 'Band', '25', '49.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('14', '5', 'Camera', '8', '599.99')",
"insert into inventory (inventory_id, store_id, product_name, quantity, price) values ('15', '5', 'Lens', '12', '199.99')"
],
"enableRunCode": true,
"envInfo": "{\"mysql\":[\"MySQL\",\"<p>\\u7248\\u672c\\uff1a<code>MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"<p>mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"<p>Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"<p>PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"stores\":[\"store_id\",\"store_name\",\"location\"],\"inventory\":[\"inventory_id\",\"store_id\",\"product_name\",\"quantity\",\"price\"]},\"rows\":{\"stores\":[[1,\"Downtown Tech\",\"New York\"],[2,\"Suburb Mall\",\"Chicago\"],[3,\"City Center\",\"Los Angeles\"],[4,\"Corner Shop\",\"Miami\"],[5,\"Plaza Store\",\"Seattle\"]],\"inventory\":[[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]]}}",
"__typename": "QuestionNode"
}
}
}