Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit81ba6c0

Browse files
authored
chore: add new lc problems (#4416)
1 parent49fea5c commit81ba6c0

File tree

8 files changed

+587
-0
lines changed

8 files changed

+587
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
comments:true
3+
difficulty:困难
4+
edit_url:https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README.md
5+
tags:
6+
-数据库
7+
---
8+
9+
<!-- problem:start-->
10+
11+
#[3554. 查找类别推荐对](https://leetcode.cn/problems/find-category-recommendation-pairs)
12+
13+
[English Version](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README_EN.md)
14+
15+
##题目描述
16+
17+
<!-- description:start-->
18+
19+
<p>表:<code>ProductPurchases</code></p>
20+
21+
<pre>
22+
+-------------+------+
23+
| Column Name | Type |
24+
+-------------+------+
25+
| user_id | int |
26+
| product_id | int |
27+
| quantity | int |
28+
+-------------+------+
29+
(user_id, product_id) 是这张表的唯一主键。
30+
每一行代表用户以特定数量购买的一种产品。
31+
</pre>
32+
33+
<p>表:<code>ProductInfo</code></p>
34+
35+
<pre>
36+
+-------------+---------+
37+
| Column Name | Type |
38+
+-------------+---------+
39+
| product_id | int |
40+
| category | varchar |
41+
| price | decimal |
42+
+-------------+---------+
43+
product_id 是这张表的唯一主键。
44+
每一行表示一件商品的类别和价格。
45+
</pre>
46+
47+
<p>亚马逊想要了解不同产品类别的购物模式。编写一个解决方案:</p>
48+
49+
<ol>
50+
<li>查找所有 <strong>类别对</strong>(其中&nbsp;<code>category1</code> &lt; <code>category2</code>)</li>
51+
<li>对于 <strong>每个类别对</strong>,确定 <strong>同时</strong> 购买了两类别产品的 <strong>不同用户</strong> 数量</li>
52+
</ol>
53+
54+
<p>如果至少有 <code>3</code> 个不同的客户购买了两个类别的产品,则类别对被视为 <strong>可报告的</strong>。</p>
55+
56+
<p>返回可报告类别对的结果表以<em>&nbsp;</em><strong>customer_count</strong><em>&nbsp;</em><strong>降序</strong><em> </em>排序,并且为了防止排序持平,以<em>&nbsp;</em><strong>category1 </strong>字典序<strong> 升序</strong>&nbsp;排序,然后以&nbsp;<strong>category2 升序</strong>&nbsp;排序。</p>
57+
58+
<p>结果格式如下所示。</p>
59+
60+
<p>&nbsp;</p>
61+
62+
<p><strongclass="example">示例:</strong></p>
63+
64+
<divclass="example-block">
65+
<p><strong>输入:</strong></p>
66+
67+
<p>ProductPurchases 表:</p>
68+
69+
<preclass="example-io">
70+
+---------+------------+----------+
71+
| user_id | product_id | quantity |
72+
+---------+------------+----------+
73+
| 1 | 101 | 2 |
74+
| 1 | 102 | 1 |
75+
| 1 | 201 | 3 |
76+
| 1 | 301 | 1 |
77+
| 2 | 101 | 1 |
78+
| 2 | 102 | 2 |
79+
| 2 | 103 | 1 |
80+
| 2 | 201 | 5 |
81+
| 3 | 101 | 2 |
82+
| 3 | 103 | 1 |
83+
| 3 | 301 | 4 |
84+
| 3 | 401 | 2 |
85+
| 4 | 101 | 1 |
86+
| 4 | 201 | 3 |
87+
| 4 | 301 | 1 |
88+
| 4 | 401 | 2 |
89+
| 5 | 102 | 2 |
90+
| 5 | 103 | 1 |
91+
| 5 | 201 | 2 |
92+
| 5 | 202 | 3 |
93+
+---------+------------+----------+
94+
</pre>
95+
96+
<p>ProductInfo 表:</p>
97+
98+
<preclass="example-io">
99+
+------------+-------------+-------+
100+
| product_id | category | price |
101+
+------------+-------------+-------+
102+
| 101 | Electronics | 100 |
103+
| 102 | Books | 20 |
104+
| 103 | Books | 35 |
105+
| 201 | Clothing | 45 |
106+
| 202 | Clothing | 60 |
107+
| 301 | Sports | 75 |
108+
| 401 | Kitchen | 50 |
109+
+------------+-------------+-------+
110+
</pre>
111+
112+
<p><strong>输出:</strong></p>
113+
114+
<preclass="example-io">
115+
+-------------+-------------+----------------+
116+
| category1 | category2 | customer_count |
117+
+-------------+-------------+----------------+
118+
| Books | Clothing | 3 |
119+
| Books | Electronics | 3 |
120+
| Clothing | Electronics | 3 |
121+
| Electronics | Sports | 3 |
122+
+-------------+-------------+----------------+
123+
</pre>
124+
125+
<p><strong>解释:</strong></p>
126+
127+
<ul>
128+
<li><strong>Books-Clothing</strong>:
129+
130+
<ul>
131+
<li>用户 1 购买来自 Books (102) 和 Clothing (201) 的商品</li>
132+
<li>用户 2 购买来自 Books (102, 103) 和 Clothing (201) 的商品</li>
133+
<li>用户 5 购买来自 Books (102, 103) 和 Clothing (201, 202) 的商品</li>
134+
<li>共计:3 个用户购买同一类别的商品</li>
135+
</ul>
136+
</li>
137+
<li><strong>Books-Electronics</strong>:
138+
<ul>
139+
<li>用户 1 购买来自 Books (102) 和 Electronics (101) 的商品</li>
140+
<li>用户 2 购买来自 Books (102, 103) 和 Electronics (101)&nbsp;的商品</li>
141+
<li>用户 3&nbsp;购买来自 Books (103) 和 Electronics (101)&nbsp;的商品</li>
142+
<li>共计:3 个消费者购买同一类别的商品</li>
143+
</ul>
144+
</li>
145+
<li><strong>Clothing-Electronics</strong>:
146+
<ul>
147+
<li>用户 1 购买来自 Clothing (201) 和 Electronics (101) 的商品</li>
148+
<li>用户 2 购买来自 Clothing (201) 和 Electronics (101) 的商品</li>
149+
<li>用户 4&nbsp;购买来自 Clothing (201) 和 Electronics (101) 的商品</li>
150+
<li>共计:3 个消费者购买同一类别的商品</li>
151+
</ul>
152+
</li>
153+
<li><strong>Electronics-Sports</strong>:
154+
<ul>
155+
<li>用户 1 购买来自 Electronics (101) 和 Sports (301) 的商品</li>
156+
<li>用户 3&nbsp;购买来自 Electronics (101) 和 Sports (301) 的商品</li>
157+
<li>用户 4&nbsp;购买来自 Electronics (101) 和 Sports (301) 的商品</li>
158+
<li>共计:3 个消费者购买同一类别的商品</li>
159+
</ul>
160+
</li>
161+
<li>其它类别对比如 Clothing-Sports(只有 2 个消费者:用户 1 和 4)和 Books-Kitchen(只有 1 个消费者:用户 3)共同的消费者少于 3 个,因此不包含在结果内。</li>
162+
163+
</ul>
164+
165+
<p>结果按&nbsp;customer_count 降序排列。由于所有对都有相同的客户数量 3,它们按 category1(然后是 category2)升序排列。</p>
166+
</div>
167+
168+
<!-- description:end-->
169+
170+
##解法
171+
172+
<!-- solution:start-->
173+
174+
###方法一
175+
176+
<!-- tabs:start-->
177+
178+
####MySQL
179+
180+
```sql
181+
182+
```
183+
184+
<!-- tabs:end-->
185+
186+
<!-- solution:end-->
187+
188+
<!-- problem:end-->
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
---
2+
comments:true
3+
difficulty:Hard
4+
edit_url:https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README_EN.md
5+
tags:
6+
-Database
7+
---
8+
9+
<!-- problem:start-->
10+
11+
#[3554. Find Category Recommendation Pairs](https://leetcode.com/problems/find-category-recommendation-pairs)
12+
13+
[中文文档](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README.md)
14+
15+
##Description
16+
17+
<!-- description:start-->
18+
19+
<p>Table: <code>ProductPurchases</code></p>
20+
21+
<pre>
22+
+-------------+------+
23+
| Column Name | Type |
24+
+-------------+------+
25+
| user_id | int |
26+
| product_id | int |
27+
| quantity | int |
28+
+-------------+------+
29+
(user_id, product_id) is the unique identifier for this table.
30+
Each row represents a purchase of a product by a user in a specific quantity.
31+
</pre>
32+
33+
<p>Table: <code>ProductInfo</code></p>
34+
35+
<pre>
36+
+-------------+---------+
37+
| Column Name | Type |
38+
+-------------+---------+
39+
| product_id | int |
40+
| category | varchar |
41+
| price | decimal |
42+
+-------------+---------+
43+
product_id is the unique identifier for this table.
44+
Each row assigns a category and price to a product.
45+
</pre>
46+
47+
<p>Amazon wants to understand shopping patterns across product categories. Write a solution to:</p>
48+
49+
<ol>
50+
<li>Find all <strong>category pairs</strong> (where <code>category1</code> &lt; <code>category2</code>)</li>
51+
<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>
52+
</ol>
53+
54+
<p>A category pair is considered <strong>reportable</strong> if at least <code>3</code> different customers have purchased products from both categories.</p>
55+
56+
<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>
57+
58+
<p>The result format is in the following example.</p>
59+
60+
<p>&nbsp;</p>
61+
<p><strongclass="example">Example:</strong></p>
62+
63+
<divclass="example-block">
64+
<p><strong>Input:</strong></p>
65+
66+
<p>ProductPurchases table:</p>
67+
68+
<preclass="example-io">
69+
+---------+------------+----------+
70+
| user_id | product_id | quantity |
71+
+---------+------------+----------+
72+
| 1 | 101 | 2 |
73+
| 1 | 102 | 1 |
74+
| 1 | 201 | 3 |
75+
| 1 | 301 | 1 |
76+
| 2 | 101 | 1 |
77+
| 2 | 102 | 2 |
78+
| 2 | 103 | 1 |
79+
| 2 | 201 | 5 |
80+
| 3 | 101 | 2 |
81+
| 3 | 103 | 1 |
82+
| 3 | 301 | 4 |
83+
| 3 | 401 | 2 |
84+
| 4 | 101 | 1 |
85+
| 4 | 201 | 3 |
86+
| 4 | 301 | 1 |
87+
| 4 | 401 | 2 |
88+
| 5 | 102 | 2 |
89+
| 5 | 103 | 1 |
90+
| 5 | 201 | 2 |
91+
| 5 | 202 | 3 |
92+
+---------+------------+----------+
93+
</pre>
94+
95+
<p>ProductInfo table:</p>
96+
97+
<preclass="example-io">
98+
+------------+-------------+-------+
99+
| product_id | category | price |
100+
+------------+-------------+-------+
101+
| 101 | Electronics | 100 |
102+
| 102 | Books | 20 |
103+
| 103 | Books | 35 |
104+
| 201 | Clothing | 45 |
105+
| 202 | Clothing | 60 |
106+
| 301 | Sports | 75 |
107+
| 401 | Kitchen | 50 |
108+
+------------+-------------+-------+
109+
</pre>
110+
111+
<p><strong>Output:</strong></p>
112+
113+
<preclass="example-io">
114+
+-------------+-------------+----------------+
115+
| category1 | category2 | customer_count |
116+
+-------------+-------------+----------------+
117+
| Books | Clothing | 3 |
118+
| Books | Electronics | 3 |
119+
| Clothing | Electronics | 3 |
120+
| Electronics | Sports | 3 |
121+
+-------------+-------------+----------------+
122+
</pre>
123+
124+
<p><strong>Explanation:</strong></p>
125+
126+
<ul>
127+
<li><strong>Books-Clothing</strong>:
128+
129+
<ul>
130+
<li>User 1 purchased products from Books (102) and Clothing (201)</li>
131+
<li>User 2 purchased products from Books (102, 103) and Clothing (201)</li>
132+
<li>User 5 purchased products from Books (102, 103) and Clothing (201, 202)</li>
133+
<li>Total: 3 customers purchased from both categories</li>
134+
</ul>
135+
</li>
136+
<li><strong>Books-Electronics</strong>:
137+
<ul>
138+
<li>User 1 purchased products from Books (102) and Electronics (101)</li>
139+
<li>User 2 purchased products from Books (102, 103) and Electronics (101)</li>
140+
<li>User 3 purchased products from Books (103) and Electronics (101)</li>
141+
<li>Total: 3 customers purchased from both categories</li>
142+
</ul>
143+
</li>
144+
<li><strong>Clothing-Electronics</strong>:
145+
<ul>
146+
<li>User 1 purchased products from Clothing (201) and Electronics (101)</li>
147+
<li>User 2 purchased products from Clothing (201) and Electronics (101)</li>
148+
<li>User 4 purchased products from Clothing (201) and Electronics (101)</li>
149+
<li>Total: 3 customers purchased from both categories</li>
150+
</ul>
151+
</li>
152+
<li><strong>Electronics-Sports</strong>:
153+
<ul>
154+
<li>User 1 purchased products from Electronics (101) and Sports (301)</li>
155+
<li>User 3 purchased products from Electronics (101) and Sports (301)</li>
156+
<li>User 4 purchased products from Electronics (101) and Sports (301)</li>
157+
<li>Total: 3 customers purchased from both categories</li>
158+
</ul>
159+
</li>
160+
<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>
161+
162+
</ul>
163+
164+
<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>
165+
</div>
166+
167+
<!-- description:end-->
168+
169+
##Solutions
170+
171+
<!-- solution:start-->
172+
173+
###Solution 1
174+
175+
<!-- tabs:start-->
176+
177+
####MySQL
178+
179+
```sql
180+
181+
```
182+
183+
<!-- tabs:end-->
184+
185+
<!-- solution:end-->
186+
187+
<!-- problem:end-->

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp