mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 15:31:43 +08:00
116 lines
21 KiB
JSON
116 lines
21 KiB
JSON
{
|
||
"data": {
|
||
"question": {
|
||
"questionId": "3891",
|
||
"questionFrontendId": "3554",
|
||
"categoryTitle": "Database",
|
||
"boundTopicId": 3680766,
|
||
"title": "Find Category Recommendation Pairs",
|
||
"titleSlug": "find-category-recommendation-pairs",
|
||
"content": "<p>Table: <code>ProductPurchases</code></p>\n\n<pre>\n+-------------+------+\n| Column Name | Type | \n+-------------+------+\n| user_id | int |\n| product_id | int |\n| quantity | int |\n+-------------+------+\n(user_id, product_id) is the unique identifier for this table. \nEach row represents a purchase of a product by a user in a specific quantity.\n</pre>\n\n<p>Table: <code>ProductInfo</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type | \n+-------------+---------+\n| product_id | int |\n| category | varchar |\n| price | decimal |\n+-------------+---------+\nproduct_id is the unique identifier for this table.\nEach row assigns a category and price to a product.\n</pre>\n\n<p>Amazon wants to understand shopping patterns across product categories. Write a solution to:</p>\n\n<ol>\n\t<li>Find all <strong>category pairs</strong> (where <code>category1</code> < <code>category2</code>)</li>\n\t<li>For <strong>each category pair</strong>, determine the number of <strong>unique</strong> <strong>customers</strong> who purchased products from <strong>both</strong> categories</li>\n</ol>\n\n<p>A category pair is considered <strong>reportable</strong> if at least <code>3</code> different customers have purchased products from both categories.</p>\n\n<p>Return <em>the result table of reportable category pairs ordered by <strong>customer_count</strong> in <strong>descending</strong> order, and in case of a tie, by <strong>category1</strong> in <strong>ascending</strong> order lexicographically, and then by <strong>category2</strong> in <strong>ascending</strong> order.</em></p>\n\n<p>The result format is in the following example.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong></p>\n\n<p>ProductPurchases table:</p>\n\n<pre class=\"example-io\">\n+---------+------------+----------+\n| user_id | product_id | quantity |\n+---------+------------+----------+\n| 1 | 101 | 2 |\n| 1 | 102 | 1 |\n| 1 | 201 | 3 |\n| 1 | 301 | 1 |\n| 2 | 101 | 1 |\n| 2 | 102 | 2 |\n| 2 | 103 | 1 |\n| 2 | 201 | 5 |\n| 3 | 101 | 2 |\n| 3 | 103 | 1 |\n| 3 | 301 | 4 |\n| 3 | 401 | 2 |\n| 4 | 101 | 1 |\n| 4 | 201 | 3 |\n| 4 | 301 | 1 |\n| 4 | 401 | 2 |\n| 5 | 102 | 2 |\n| 5 | 103 | 1 |\n| 5 | 201 | 2 |\n| 5 | 202 | 3 |\n+---------+------------+----------+\n</pre>\n\n<p>ProductInfo table:</p>\n\n<pre class=\"example-io\">\n+------------+-------------+-------+\n| product_id | category | price |\n+------------+-------------+-------+\n| 101 | Electronics | 100 |\n| 102 | Books | 20 |\n| 103 | Books | 35 |\n| 201 | Clothing | 45 |\n| 202 | Clothing | 60 |\n| 301 | Sports | 75 |\n| 401 | Kitchen | 50 |\n+------------+-------------+-------+\n</pre>\n\n<p><strong>Output:</strong></p>\n\n<pre class=\"example-io\">\n+-------------+-------------+----------------+\n| category1 | category2 | customer_count |\n+-------------+-------------+----------------+\n| Books | Clothing | 3 |\n| Books | Electronics | 3 |\n| Clothing | Electronics | 3 |\n| Electronics | Sports | 3 |\n+-------------+-------------+----------------+\n</pre>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n\t<li><strong>Books-Clothing</strong>:\n\n\t<ul>\n\t\t<li>User 1 purchased products from Books (102) and Clothing (201)</li>\n\t\t<li>User 2 purchased products from Books (102, 103) and Clothing (201)</li>\n\t\t<li>User 5 purchased products from Books (102, 103) and Clothing (201, 202)</li>\n\t\t<li>Total: 3 customers purchased from both categories</li>\n\t</ul>\n\t</li>\n\t<li><strong>Books-Electronics</strong>:\n\t<ul>\n\t\t<li>User 1 purchased products from Books (102) and Electronics (101)</li>\n\t\t<li>User 2 purchased products from Books (102, 103) and Electronics (101)</li>\n\t\t<li>User 3 purchased products from Books (103) and Electronics (101)</li>\n\t\t<li>Total: 3 customers purchased from both categories</li>\n\t</ul>\n\t</li>\n\t<li><strong>Clothing-Electronics</strong>:\n\t<ul>\n\t\t<li>User 1 purchased products from Clothing (201) and Electronics (101)</li>\n\t\t<li>User 2 purchased products from Clothing (201) and Electronics (101)</li>\n\t\t<li>User 4 purchased products from Clothing (201) and Electronics (101)</li>\n\t\t<li>Total: 3 customers purchased from both categories</li>\n\t</ul>\n\t</li>\n\t<li><strong>Electronics-Sports</strong>:\n\t<ul>\n\t\t<li>User 1 purchased products from Electronics (101) and Sports (301)</li>\n\t\t<li>User 3 purchased products from Electronics (101) and Sports (301)</li>\n\t\t<li>User 4 purchased products from Electronics (101) and Sports (301)</li>\n\t\t<li>Total: 3 customers purchased from both categories</li>\n\t</ul>\n\t</li>\n\t<li>Other category pairs like Clothing-Sports (only 2 customers: Users 1 and 4) and Books-Kitchen (only 1 customer: User 3) have fewer than 3 shared customers and are not included in the result.</li>\n</ul>\n\n<p>The result is ordered by customer_count in descending order. Since all pairs have the same customer_count of 3, they are ordered by category1 (then category2) in ascending order.</p>\n</div>\n",
|
||
"translatedTitle": "查找类别推荐对",
|
||
"translatedContent": "<p>表:<code>ProductPurchases</code></p>\n\n<pre>\n+-------------+------+\n| Column Name | Type | \n+-------------+------+\n| user_id | int |\n| product_id | int |\n| quantity | int |\n+-------------+------+\n(user_id, product_id) 是这张表的唯一主键。\n每一行代表用户以特定数量购买的一种产品。\n</pre>\n\n<p>表:<code>ProductInfo</code></p>\n\n<pre>\n+-------------+---------+\n| Column Name | Type | \n+-------------+---------+\n| product_id | int |\n| category | varchar |\n| price | decimal |\n+-------------+---------+\nproduct_id 是这张表的唯一主键。\n每一行表示一件商品的类别和价格。\n</pre>\n\n<p>亚马逊想要了解不同产品类别的购物模式。编写一个解决方案:</p>\n\n<ol>\n\t<li>查找所有 <strong>类别对</strong>(其中 <code>category1</code> < <code>category2</code>)</li>\n\t<li>对于 <strong>每个类别对</strong>,确定 <strong>同时</strong> 购买了两类别产品的 <strong>不同用户</strong> 数量</li>\n</ol>\n\n<p>如果至少有 <code>3</code> 个不同的客户购买了两个类别的产品,则类别对被视为 <strong>可报告的</strong>。</p>\n\n<p>返回可报告类别对的结果表以<em> </em><strong>customer_count</strong><em> </em><strong>降序</strong><em> </em>排序,并且为了防止排序持平,以<em> </em><strong>category1 </strong>字典序<strong> 升序</strong> 排序,然后以 <strong>category2 升序</strong> 排序。</p>\n\n<p>结果格式如下所示。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>输入:</strong></p>\n\n<p>ProductPurchases 表:</p>\n\n<pre class=\"example-io\">\n+---------+------------+----------+\n| user_id | product_id | quantity |\n+---------+------------+----------+\n| 1 | 101 | 2 |\n| 1 | 102 | 1 |\n| 1 | 201 | 3 |\n| 1 | 301 | 1 |\n| 2 | 101 | 1 |\n| 2 | 102 | 2 |\n| 2 | 103 | 1 |\n| 2 | 201 | 5 |\n| 3 | 101 | 2 |\n| 3 | 103 | 1 |\n| 3 | 301 | 4 |\n| 3 | 401 | 2 |\n| 4 | 101 | 1 |\n| 4 | 201 | 3 |\n| 4 | 301 | 1 |\n| 4 | 401 | 2 |\n| 5 | 102 | 2 |\n| 5 | 103 | 1 |\n| 5 | 201 | 2 |\n| 5 | 202 | 3 |\n+---------+------------+----------+\n</pre>\n\n<p>ProductInfo 表:</p>\n\n<pre class=\"example-io\">\n+------------+-------------+-------+\n| product_id | category | price |\n+------------+-------------+-------+\n| 101 | Electronics | 100 |\n| 102 | Books | 20 |\n| 103 | Books | 35 |\n| 201 | Clothing | 45 |\n| 202 | Clothing | 60 |\n| 301 | Sports | 75 |\n| 401 | Kitchen | 50 |\n+------------+-------------+-------+\n</pre>\n\n<p><strong>输出:</strong></p>\n\n<pre class=\"example-io\">\n+-------------+-------------+----------------+\n| category1 | category2 | customer_count |\n+-------------+-------------+----------------+\n| Books | Clothing | 3 |\n| Books | Electronics | 3 |\n| Clothing | Electronics | 3 |\n| Electronics | Sports | 3 |\n+-------------+-------------+----------------+\n</pre>\n\n<p><strong>解释:</strong></p>\n\n<ul>\n\t<li><strong>Books-Clothing</strong>:\n\n\t<ul>\n\t\t<li>用户 1 购买来自 Books (102) 和 Clothing (201) 的商品</li>\n\t\t<li>用户 2 购买来自 Books (102, 103) 和 Clothing (201) 的商品</li>\n\t\t<li>用户 5 购买来自 Books (102, 103) 和 Clothing (201, 202) 的商品</li>\n\t\t<li>共计:3 个用户购买同一类别的商品</li>\n\t</ul>\n\t</li>\n\t<li><strong>Books-Electronics</strong>:\n\t<ul>\n\t\t<li>用户 1 购买来自 Books (102) 和 Electronics (101) 的商品</li>\n\t\t<li>用户 2 购买来自 Books (102, 103) 和 Electronics (101) 的商品</li>\n\t\t<li>用户 3 购买来自 Books (103) 和 Electronics (101) 的商品</li>\n\t\t<li>共计:3 个消费者购买同一类别的商品</li>\n\t</ul>\n\t</li>\n\t<li><strong>Clothing-Electronics</strong>:\n\t<ul>\n\t\t<li>用户 1 购买来自 Clothing (201) 和 Electronics (101) 的商品</li>\n\t\t<li>用户 2 购买来自 Clothing (201) 和 Electronics (101) 的商品</li>\n\t\t<li>用户 4 购买来自 Clothing (201) 和 Electronics (101) 的商品</li>\n\t\t<li>共计:3 个消费者购买同一类别的商品</li>\n\t</ul>\n\t</li>\n\t<li><strong>Electronics-Sports</strong>:\n\t<ul>\n\t\t<li>用户 1 购买来自 Electronics (101) 和 Sports (301) 的商品</li>\n\t\t<li>用户 3 购买来自 Electronics (101) 和 Sports (301) 的商品</li>\n\t\t<li>用户 4 购买来自 Electronics (101) 和 Sports (301) 的商品</li>\n\t\t<li>共计:3 个消费者购买同一类别的商品</li>\n\t</ul>\n\t</li>\n\t<li>其它类别对比如 Clothing-Sports(只有 2 个消费者:用户 1 和 4)和 Books-Kitchen(只有 1 个消费者:用户 3)共同的消费者少于 3 个,因此不包含在结果内。</li>\n</ul>\n\n<p>结果按 customer_count 降序排列。由于所有对都有相同的客户数量 3,它们按 category1(然后是 category2)升序排列。</p>\n</div>\n",
|
||
"isPaidOnly": false,
|
||
"difficulty": "Hard",
|
||
"likes": 0,
|
||
"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": [
|
||
{
|
||
"name": "Database",
|
||
"slug": "database",
|
||
"translatedName": "数据库",
|
||
"__typename": "TopicTagNode"
|
||
}
|
||
],
|
||
"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_category_recommendation_pairs(product_purchases: pd.DataFrame, product_info: pd.DataFrame) -> pd.DataFrame:\n ",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "PostgreSQL",
|
||
"langSlug": "postgresql",
|
||
"code": "-- Write your PostgreSQL query statement below",
|
||
"__typename": "CodeSnippetNode"
|
||
}
|
||
],
|
||
"stats": "{\"totalAccepted\": \"88\", \"totalSubmission\": \"107\", \"totalAcceptedRaw\": 88, \"totalSubmissionRaw\": 107, \"acRate\": \"82.2%\"}",
|
||
"hints": [],
|
||
"solution": null,
|
||
"status": null,
|
||
"sampleTestCase": "{\"headers\":{\"ProductPurchases\":[\"user_id\",\"product_id\",\"quantity\"],\"ProductInfo\":[\"product_id\",\"category\",\"price\"]},\"rows\":{\"ProductPurchases\":[[1,101,2],[1,102,1],[1,201,3],[1,301,1],[2,101,1],[2,102,2],[2,103,1],[2,201,5],[3,101,2],[3,103,1],[3,301,4],[3,401,2],[4,101,1],[4,201,3],[4,301,1],[4,401,2],[5,102,2],[5,103,1],[5,201,2],[5,202,3]],\"ProductInfo\":[[101,\"Electronics\",100],[102,\"Books\",20],[103,\"Books\",35],[201,\"Clothing\",45],[202,\"Clothing\",60],[301,\"Sports\",75],[401,\"Kitchen\",50]]}}",
|
||
"metaData": "{\"mysql\":[\"CREATE TABLE if not exists ProductPurchases (\\n user_id INT,\\n product_id INT,\\n quantity INT\\n)\",\"CREATE TABLE if not exists ProductInfo (\\n product_id INT,\\n category VARCHAR(100),\\n price DECIMAL(10, 2)\\n)\"],\"mssql\":[\"CREATE TABLE ProductPurchases (\\n user_id INT,\\n product_id INT,\\n quantity INT\\n)\",\"CREATE TABLE ProductInfo (\\n product_id INT,\\n category VARCHAR(100),\\n price DECIMAL(10, 2)\\n)\"],\"oraclesql\":[\"CREATE TABLE ProductPurchases (\\n user_id NUMBER,\\n product_id NUMBER,\\n quantity NUMBER\\n)\",\"CREATE TABLE ProductInfo (\\n product_id NUMBER,\\n category VARCHAR2(100),\\n price NUMBER(10, 2)\\n)\"],\"postgresql\":[\"CREATE TABLE ProductPurchases (\\n user_id INTEGER,\\n product_id INTEGER,\\n quantity INTEGER\\n);\",\"CREATE TABLE ProductInfo (\\n product_id INTEGER,\\n category VARCHAR(100),\\n price NUMERIC(10, 2)\\n);\"],\"name\":\"find_category_recommendation_pairs\",\"pythondata\":[\"ProductPurchases = pd.DataFrame({\\n \\\"user_id\\\": pd.Series(dtype='int64'),\\n \\\"product_id\\\": pd.Series(dtype='int64'),\\n \\\"quantity\\\": pd.Series(dtype='int64')\\n})\",\"ProductInfo= pd.DataFrame({\\n \\\"product_id\\\": pd.Series(dtype='int64'),\\n \\\"category\\\": pd.Series(dtype='string'),\\n \\\"price\\\": pd.Series(dtype='float64') # Reflects NUMBER(10, 2)\\n})\"],\"database\":true,\"database_schema\":{\"ProductPurchases\":{\"user_id\":\"INT\",\"product_id\":\"INT\",\"quantity\":\"INT\"},\"ProductInfo\":{\"product_id\":\"INT\",\"category\":\"VARCHAR(100)\",\"price\":\"DECIMAL(10, 2)\"}}}",
|
||
"judgerAvailable": true,
|
||
"judgeType": "large",
|
||
"mysqlSchemas": [
|
||
"CREATE TABLE if not exists ProductPurchases (\n user_id INT,\n product_id INT,\n quantity INT\n)",
|
||
"CREATE TABLE if not exists ProductInfo (\n product_id INT,\n category VARCHAR(100),\n price DECIMAL(10, 2)\n)",
|
||
"Truncate table ProductPurchases",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('1', '101', '2')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('1', '102', '1')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('1', '201', '3')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('1', '301', '1')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('2', '101', '1')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('2', '102', '2')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('2', '103', '1')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('2', '201', '5')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('3', '101', '2')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('3', '103', '1')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('3', '301', '4')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('3', '401', '2')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('4', '101', '1')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('4', '201', '3')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('4', '301', '1')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('4', '401', '2')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('5', '102', '2')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('5', '103', '1')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('5', '201', '2')",
|
||
"insert into ProductPurchases (user_id, product_id, quantity) values ('5', '202', '3')",
|
||
"Truncate table ProductInfo",
|
||
"insert into ProductInfo (product_id, category, price) values ('101', 'Electronics', '100')",
|
||
"insert into ProductInfo (product_id, category, price) values ('102', 'Books', '20')",
|
||
"insert into ProductInfo (product_id, category, price) values ('103', 'Books', '35')",
|
||
"insert into ProductInfo (product_id, category, price) values ('201', 'Clothing', '45')",
|
||
"insert into ProductInfo (product_id, category, price) values ('202', 'Clothing', '60')",
|
||
"insert into ProductInfo (product_id, category, price) values ('301', 'Sports', '75')",
|
||
"insert into ProductInfo (product_id, category, price) values ('401', 'Kitchen', '50')"
|
||
],
|
||
"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\":{\"ProductPurchases\":[\"user_id\",\"product_id\",\"quantity\"],\"ProductInfo\":[\"product_id\",\"category\",\"price\"]},\"rows\":{\"ProductPurchases\":[[1,101,2],[1,102,1],[1,201,3],[1,301,1],[2,101,1],[2,102,2],[2,103,1],[2,201,5],[3,101,2],[3,103,1],[3,301,4],[3,401,2],[4,101,1],[4,201,3],[4,301,1],[4,401,2],[5,102,2],[5,103,1],[5,201,2],[5,202,3]],\"ProductInfo\":[[101,\"Electronics\",100],[102,\"Books\",20],[103,\"Books\",35],[201,\"Clothing\",45],[202,\"Clothing\",60],[301,\"Sports\",75],[401,\"Kitchen\",50]]}}",
|
||
"__typename": "QuestionNode"
|
||
}
|
||
}
|
||
} |