Skip to content

chore: add new lc problems #4416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions solution/3500-3599/3554.Find Category Recommendation Pairs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
---
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README.md
tags:
- 数据库
---

<!-- problem:start -->

# [3554. 查找类别推荐对](https://leetcode.cn/problems/find-category-recommendation-pairs)

[English Version](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README_EN.md)

## 题目描述

<!-- description:start -->

<p>表:<code>ProductPurchases</code></p>

<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| product_id | int |
| quantity | int |
+-------------+------+
(user_id, product_id) 是这张表的唯一主键。
每一行代表用户以特定数量购买的一种产品。
</pre>

<p>表:<code>ProductInfo</code></p>

<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| category | varchar |
| price | decimal |
+-------------+---------+
product_id 是这张表的唯一主键。
每一行表示一件商品的类别和价格。
</pre>

<p>亚马逊想要了解不同产品类别的购物模式。编写一个解决方案:</p>

<ol>
<li>查找所有 <strong>类别对</strong>(其中&nbsp;<code>category1</code> &lt; <code>category2</code>)</li>
<li>对于 <strong>每个类别对</strong>,确定 <strong>同时</strong> 购买了两类别产品的 <strong>不同用户</strong> 数量</li>
</ol>

<p>如果至少有 <code>3</code> 个不同的客户购买了两个类别的产品,则类别对被视为 <strong>可报告的</strong>。</p>

<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>

<p>结果格式如下所示。</p>

<p>&nbsp;</p>

<p><strong class="example">示例:</strong></p>

<div class="example-block">
<p><strong>输入:</strong></p>

<p>ProductPurchases 表:</p>

<pre class="example-io">
+---------+------------+----------+
| user_id | product_id | quantity |
+---------+------------+----------+
| 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 |
+---------+------------+----------+
</pre>

<p>ProductInfo 表:</p>

<pre class="example-io">
+------------+-------------+-------+
| product_id | category | price |
+------------+-------------+-------+
| 101 | Electronics | 100 |
| 102 | Books | 20 |
| 103 | Books | 35 |
| 201 | Clothing | 45 |
| 202 | Clothing | 60 |
| 301 | Sports | 75 |
| 401 | Kitchen | 50 |
+------------+-------------+-------+
</pre>

<p><strong>输出:</strong></p>

<pre class="example-io">
+-------------+-------------+----------------+
| category1 | category2 | customer_count |
+-------------+-------------+----------------+
| Books | Clothing | 3 |
| Books | Electronics | 3 |
| Clothing | Electronics | 3 |
| Electronics | Sports | 3 |
+-------------+-------------+----------------+
</pre>

<p><strong>解释:</strong></p>

<ul>
<li><strong>Books-Clothing</strong>:

<ul>
<li>用户 1 购买来自 Books (102) 和 Clothing (201) 的商品</li>
<li>用户 2 购买来自 Books (102, 103) 和 Clothing (201) 的商品</li>
<li>用户 5 购买来自 Books (102, 103) 和 Clothing (201, 202) 的商品</li>
<li>共计:3 个用户购买同一类别的商品</li>
</ul>
</li>
<li><strong>Books-Electronics</strong>:
<ul>
<li>用户 1 购买来自 Books (102) 和 Electronics (101) 的商品</li>
<li>用户 2 购买来自 Books (102, 103) 和 Electronics (101)&nbsp;的商品</li>
<li>用户 3&nbsp;购买来自 Books (103) 和 Electronics (101)&nbsp;的商品</li>
<li>共计:3 个消费者购买同一类别的商品</li>
</ul>
</li>
<li><strong>Clothing-Electronics</strong>:
<ul>
<li>用户 1 购买来自 Clothing (201) 和 Electronics (101) 的商品</li>
<li>用户 2 购买来自 Clothing (201) 和 Electronics (101) 的商品</li>
<li>用户 4&nbsp;购买来自 Clothing (201) 和 Electronics (101) 的商品</li>
<li>共计:3 个消费者购买同一类别的商品</li>
</ul>
</li>
<li><strong>Electronics-Sports</strong>:
<ul>
<li>用户 1 购买来自 Electronics (101) 和 Sports (301) 的商品</li>
<li>用户 3&nbsp;购买来自 Electronics (101) 和 Sports (301) 的商品</li>
<li>用户 4&nbsp;购买来自 Electronics (101) 和 Sports (301) 的商品</li>
<li>共计:3 个消费者购买同一类别的商品</li>
</ul>
</li>
<li>其它类别对比如 Clothing-Sports(只有 2 个消费者:用户 1 和 4)和 Books-Kitchen(只有 1 个消费者:用户 3)共同的消费者少于 3 个,因此不包含在结果内。</li>

</ul>

<p>结果按&nbsp;customer_count 降序排列。由于所有对都有相同的客户数量 3,它们按 category1(然后是 category2)升序排列。</p>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### MySQL

```sql

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
---
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README_EN.md
tags:
- Database
---

<!-- problem:start -->

# [3554. Find Category Recommendation Pairs](https://leetcode.com/problems/find-category-recommendation-pairs)

[中文文档](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README.md)

## Description

<!-- description:start -->

<p>Table: <code>ProductPurchases</code></p>

<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| product_id | int |
| quantity | int |
+-------------+------+
(user_id, product_id) is the unique identifier for this table.
Each row represents a purchase of a product by a user in a specific quantity.
</pre>

<p>Table: <code>ProductInfo</code></p>

<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| category | varchar |
| price | decimal |
+-------------+---------+
product_id is the unique identifier for this table.
Each row assigns a category and price to a product.
</pre>

<p>Amazon wants to understand shopping patterns across product categories. Write a solution to:</p>

<ol>
<li>Find all <strong>category pairs</strong> (where <code>category1</code> &lt; <code>category2</code>)</li>
<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>
</ol>

<p>A category pair is considered <strong>reportable</strong> if at least <code>3</code> different customers have purchased products from both categories.</p>

<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>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p>ProductPurchases table:</p>

<pre class="example-io">
+---------+------------+----------+
| user_id | product_id | quantity |
+---------+------------+----------+
| 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 |
+---------+------------+----------+
</pre>

<p>ProductInfo table:</p>

<pre class="example-io">
+------------+-------------+-------+
| product_id | category | price |
+------------+-------------+-------+
| 101 | Electronics | 100 |
| 102 | Books | 20 |
| 103 | Books | 35 |
| 201 | Clothing | 45 |
| 202 | Clothing | 60 |
| 301 | Sports | 75 |
| 401 | Kitchen | 50 |
+------------+-------------+-------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+-------------+-------------+----------------+
| category1 | category2 | customer_count |
+-------------+-------------+----------------+
| Books | Clothing | 3 |
| Books | Electronics | 3 |
| Clothing | Electronics | 3 |
| Electronics | Sports | 3 |
+-------------+-------------+----------------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li><strong>Books-Clothing</strong>:

<ul>
<li>User 1 purchased products from Books (102) and Clothing (201)</li>
<li>User 2 purchased products from Books (102, 103) and Clothing (201)</li>
<li>User 5 purchased products from Books (102, 103) and Clothing (201, 202)</li>
<li>Total: 3 customers purchased from both categories</li>
</ul>
</li>
<li><strong>Books-Electronics</strong>:
<ul>
<li>User 1 purchased products from Books (102) and Electronics (101)</li>
<li>User 2 purchased products from Books (102, 103) and Electronics (101)</li>
<li>User 3 purchased products from Books (103) and Electronics (101)</li>
<li>Total: 3 customers purchased from both categories</li>
</ul>
</li>
<li><strong>Clothing-Electronics</strong>:
<ul>
<li>User 1 purchased products from Clothing (201) and Electronics (101)</li>
<li>User 2 purchased products from Clothing (201) and Electronics (101)</li>
<li>User 4 purchased products from Clothing (201) and Electronics (101)</li>
<li>Total: 3 customers purchased from both categories</li>
</ul>
</li>
<li><strong>Electronics-Sports</strong>:
<ul>
<li>User 1 purchased products from Electronics (101) and Sports (301)</li>
<li>User 3 purchased products from Electronics (101) and Sports (301)</li>
<li>User 4 purchased products from Electronics (101) and Sports (301)</li>
<li>Total: 3 customers purchased from both categories</li>
</ul>
</li>
<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>

</ul>

<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>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### MySQL

```sql

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading