1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-06 16:01:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2025-04-24 21:29:06 +08:00
parent 3a5ef731a2
commit d48ef04fb6
56 changed files with 16204 additions and 9836 deletions

View File

@@ -0,0 +1,109 @@
{
"data": {
"question": {
"questionId": "3865",
"questionFrontendId": "3521",
"categoryTitle": "Database",
"boundTopicId": 3653631,
"title": "Find Product Recommendation Pairs",
"titleSlug": "find-product-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 key 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 primary key for this table.\nEach row assigns a category and price to a product.\n</pre>\n\n<p>Amazon wants to implement the <strong>Customers who bought this also bought...</strong> feature based on <strong>co-purchase patterns</strong>. Write a solution to :</p>\n\n<ol>\n\t<li>Identify <strong>distinct</strong> product pairs frequently <strong>purchased together by the same customers</strong> (where <code>product1_id</code> &lt; <code>product2_id</code>)</li>\n\t<li>For <strong>each product pair</strong>, determine how many customers purchased <strong>both</strong> products</li>\n</ol>\n\n<p><strong>A product pair </strong>is considered for recommendation <strong>if</strong> <strong>at least</strong> <code>3</code> <strong>different</strong> customers have purchased <strong>both products</strong>.</p>\n\n<p>Return <em>the </em><em>result table ordered by <strong>customer_count</strong> in <strong>descending</strong> order, and in case of a tie, by </em><code>product1_id</code><em> in <strong>ascending</strong> order, and then by </em><code>product2_id</code><em> 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>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 | 103 | 3 |\n| 2 | 101 | 1 |\n| 2 | 102 | 5 |\n| 2 | 104 | 1 |\n| 3 | 101 | 2 |\n| 3 | 103 | 1 |\n| 3 | 105 | 4 |\n| 4 | 101 | 1 |\n| 4 | 102 | 1 |\n| 4 | 103 | 2 |\n| 4 | 104 | 3 |\n| 5 | 102 | 2 |\n| 5 | 104 | 1 |\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 | Clothing | 35 |\n| 104 | Kitchen | 50 |\n| 105 | Sports | 75 |\n+------------+-------------+-------+\n</pre>\n\n<p><strong>Output:</strong></p>\n\n<pre class=\"example-io\">\n+-------------+-------------+-------------------+-------------------+----------------+\n| product1_id | product2_id | product1_category | product2_category | customer_count |\n+-------------+-------------+-------------------+-------------------+----------------+\n| 101 | 102 | Electronics | Books | 3 |\n| 101 | 103 | Electronics | Clothing | 3 |\n| 102 | 104 | Books | Kitchen | 3 |\n+-------------+-------------+-------------------+-------------------+----------------+\n</pre>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n\t<li><strong>Product pair (101, 102):</strong>\n\n\t<ul>\n\t\t<li>Purchased by users 1, 2, and 4 (3 customers)</li>\n\t\t<li>Product 101 is in Electronics category</li>\n\t\t<li>Product 102 is in Books category</li>\n\t</ul>\n\t</li>\n\t<li><strong>Product pair (101, 103):</strong>\n\t<ul>\n\t\t<li>Purchased by users 1, 3, and 4 (3 customers)</li>\n\t\t<li>Product 101 is in Electronics category</li>\n\t\t<li>Product 103 is in Clothing category</li>\n\t</ul>\n\t</li>\n\t<li><strong>Product pair (102, 104):</strong>\n\t<ul>\n\t\t<li>Purchased by users 2, 4, and 5 (3 customers)</li>\n\t\t<li>Product 102 is in Books category</li>\n\t\t<li>Product 104 is in Kitchen category</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>The result is ordered by customer_count in descending order. For pairs with the same customer_count, they are ordered by product1_id and then product2_id 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>亚马逊希望根据 <strong>共同购买模式</strong> 实现 “<strong>购买此商品的用户还购买了...</strong>” 功能。编写一个解决方案以实现:</p>\n\n<ol>\n\t<li>识别 <strong>被同一客户一起频繁购买的</strong> <strong>不同</strong> 产品对(其中&nbsp;<code>product1_id</code> &lt; <code>product2_id</code></li>\n\t<li>对于 <strong>每个产品对</strong>,确定有多少客户购买了这两种产品</li>\n</ol>\n\n<p>如果 <strong>至少有</strong> <code>3</code> <strong>位不同的</strong> 客户同时购买了这两种产品,则认为该&nbsp;<strong>产品对&nbsp;</strong>适合推荐。</p>\n\n<p>返回结果表以<em>&nbsp;</em><strong>customer_count</strong>&nbsp; <strong>降序&nbsp;</strong>排序,并且为了避免排序持平,以&nbsp;<code>product1_id</code><em> </em><strong>升序&nbsp;</strong>排序,并以<em>&nbsp;</em><code>product2_id</code><em> </em><strong>升序 </strong>排序。</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>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 | 103 | 3 |\n| 2 | 101 | 1 |\n| 2 | 102 | 5 |\n| 2 | 104 | 1 |\n| 3 | 101 | 2 |\n| 3 | 103 | 1 |\n| 3 | 105 | 4 |\n| 4 | 101 | 1 |\n| 4 | 102 | 1 |\n| 4 | 103 | 2 |\n| 4 | 104 | 3 |\n| 5 | 102 | 2 |\n| 5 | 104 | 1 |\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 | Clothing | 35 |\n| 104 | Kitchen | 50 |\n| 105 | Sports | 75 |\n+------------+-------------+-------+\n</pre>\n\n<p><strong>输出:</strong></p>\n\n<pre class=\"example-io\">\n+-------------+-------------+-------------------+-------------------+----------------+\n| product1_id | product2_id | product1_category | product2_category | customer_count |\n+-------------+-------------+-------------------+-------------------+----------------+\n| 101 | 102 | Electronics | Books | 3 |\n| 101 | 103 | Electronics | Clothing | 3 |\n| 102 | 104 | Books | Kitchen | 3 |\n+-------------+-------------+-------------------+-------------------+----------------+\n</pre>\n\n<p><strong>解释:</strong></p>\n\n<ul>\n\t<li><strong>产品对 (101, 102)</strong>\n\n\t<ul>\n\t\t<li>被用户 12 和 4 购买3 个消费者)</li>\n\t\t<li>产品 101 属于电子商品类别</li>\n\t\t<li>产品 102 属于图书类别</li>\n\t</ul>\n\t</li>\n\t<li><strong>产品对 (101, 103)</strong>\n\t<ul>\n\t\t<li>被用户 13 和 4 购买3 个消费者)</li>\n\t\t<li>产品 101 属于电子商品类别</li>\n\t\t<li>产品 103 属于服装类别</li>\n\t</ul>\n\t</li>\n\t<li><strong>产品对 (102, 104)</strong>\n\t<ul>\n\t\t<li>被用户 24 和 5 购买3 个消费者)</li>\n\t\t<li>产品 102 属于图书类别</li>\n\t\t<li>产品 104 属于厨房用品类别</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>结果以 customer_count 降序排序。对于有相同&nbsp;customer_count 的产品对,将它们以&nbsp;product1_id 升序排序,然后以 product2_id 升序排序。</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": [
{
"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_product_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\": \"172\", \"totalSubmission\": \"231\", \"totalAcceptedRaw\": 172, \"totalSubmissionRaw\": 231, \"acRate\": \"74.5%\"}",
"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,103,3],[2,101,1],[2,102,5],[2,104,1],[3,101,2],[3,103,1],[3,105,4],[4,101,1],[4,102,1],[4,103,2],[4,104,3],[5,102,2],[5,104,1]],\"ProductInfo\":[[101,\"Electronics\",100],[102,\"Books\",20],[103,\"Clothing\",35],[104,\"Kitchen\",50],[105,\"Sports\",75]]}}",
"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)\"],\"database\":true,\"name\":\"find_product_recommendation_pairs\",\"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);\"],\"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_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', '103', '3')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('2', '101', '1')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('2', '102', '5')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('2', '104', '1')",
"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', '105', '4')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('4', '101', '1')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('4', '102', '1')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('4', '103', '2')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('4', '104', '3')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('5', '102', '2')",
"insert into ProductPurchases (user_id, product_id, quantity) values ('5', '104', '1')",
"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', 'Clothing', '35')",
"insert into ProductInfo (product_id, category, price) values ('104', 'Kitchen', '50')",
"insert into ProductInfo (product_id, category, price) values ('105', 'Sports', '75')"
],
"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,103,3],[2,101,1],[2,102,5],[2,104,1],[3,101,2],[3,103,1],[3,105,4],[4,101,1],[4,102,1],[4,103,2],[4,104,3],[5,102,2],[5,104,1]],\"ProductInfo\":[[101,\"Electronics\",100],[102,\"Books\",20],[103,\"Clothing\",35],[104,\"Kitchen\",50],[105,\"Sports\",75]]}}",
"__typename": "QuestionNode"
}
}
}