diff --git a/solution/3500-3599/3554.Find Category Recommendation Pairs/README.md b/solution/3500-3599/3554.Find Category Recommendation Pairs/README.md new file mode 100644 index 0000000000000..4d6311b2874fa --- /dev/null +++ b/solution/3500-3599/3554.Find Category Recommendation Pairs/README.md @@ -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: + - 数据库 +--- + + + +# [3554. 查找类别推荐对](https://leetcode.cn/problems/find-category-recommendation-pairs) + +[English Version](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README_EN.md) + +## 题目描述 + + + +

表:ProductPurchases

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

表:ProductInfo

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

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

+ +
    +
  1. 查找所有 类别对(其中 category1 < category2
  2. +
  3. 对于 每个类别对,确定 同时 购买了两类别产品的 不同用户 数量
  4. +
+ +

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

+ +

返回可报告类别对的结果表以 customer_count 降序 排序,并且为了防止排序持平,以 category1 字典序 升序 排序,然后以 category2 升序 排序。

+ +

结果格式如下所示。

+ +

 

+ +

示例:

+ +
+

输入:

+ +

ProductPurchases 表:

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

ProductInfo 表:

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

输出:

+ +
++-------------+-------------+----------------+
+| category1   | category2   | customer_count |
++-------------+-------------+----------------+
+| Books       | Clothing    | 3              |
+| Books       | Electronics | 3              |
+| Clothing    | Electronics | 3              |
+| Electronics | Sports      | 3              |
++-------------+-------------+----------------+
+
+ +

解释:

+ + + +

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

+
+ + + +## 解法 + + + +### 方法一 + + + +#### MySQL + +```sql + +``` + + + + + + diff --git a/solution/3500-3599/3554.Find Category Recommendation Pairs/README_EN.md b/solution/3500-3599/3554.Find Category Recommendation Pairs/README_EN.md new file mode 100644 index 0000000000000..ce82e1fb6f5e3 --- /dev/null +++ b/solution/3500-3599/3554.Find Category Recommendation Pairs/README_EN.md @@ -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 +--- + + + +# [3554. Find Category Recommendation Pairs](https://leetcode.com/problems/find-category-recommendation-pairs) + +[中文文档](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README.md) + +## Description + + + +

Table: ProductPurchases

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

Table: ProductInfo

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

Amazon wants to understand shopping patterns across product categories. Write a solution to:

+ +
    +
  1. Find all category pairs (where category1 < category2)
  2. +
  3. For each category pair, determine the number of unique customers who purchased products from both categories
  4. +
+ +

A category pair is considered reportable if at least 3 different customers have purchased products from both categories.

+ +

Return the result table of reportable category pairs ordered by customer_count in descending order, and in case of a tie, by category1 in ascending order lexicographically, and then by category2 in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

ProductPurchases table:

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

ProductInfo table:

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

Output:

+ +
++-------------+-------------+----------------+
+| category1   | category2   | customer_count |
++-------------+-------------+----------------+
+| Books       | Clothing    | 3              |
+| Books       | Electronics | 3              |
+| Clothing    | Electronics | 3              |
+| Electronics | Sports      | 3              |
++-------------+-------------+----------------+
+
+ +

Explanation:

+ + + +

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.

+
+ + + +## Solutions + + + +### Solution 1 + + + +#### MySQL + +```sql + +``` + + + + + + diff --git a/solution/3500-3599/3555.Smallest Subarray to Sort in Every Sliding Window/README.md b/solution/3500-3599/3555.Smallest Subarray to Sort in Every Sliding Window/README.md new file mode 100644 index 0000000000000..7d762c126b8fc --- /dev/null +++ b/solution/3500-3599/3555.Smallest Subarray to Sort in Every Sliding Window/README.md @@ -0,0 +1,104 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3555.Smallest%20Subarray%20to%20Sort%20in%20Every%20Sliding%20Window/README.md +--- + + + +# [3555. 排序每个滑动窗口中最小的子数组 🔒](https://leetcode.cn/problems/smallest-subarray-to-sort-in-every-sliding-window) + +[English Version](/solution/3500-3599/3555.Smallest%20Subarray%20to%20Sort%20in%20Every%20Sliding%20Window/README_EN.md) + +## 题目描述 + + + +

给定一个整数数组 nums 和一个整数 k

+ +

对于每个长度为 k 的连续 子数组,确定必须排序的连续段的最小长度,以便整个窗口成为 非递减 的;如果窗口已经排序,则其所需长度为零。

+ +

返回一个长度为 n − k + 1 的数组,其中每个元素对应其窗口的答案。

+ +

 

+ +

示例 1:

+ +
+

输入:nums = [1,3,2,4,5], k = 3

+ +

输出:[2,2,0]

+ +

解释:

+ + +
+ +

示例 2:

+ +
+

输入:nums = [5,4,3,2,1], k = 4

+ +

输出:[4,4]

+ +

解释:

+ + +
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3500-3599/3555.Smallest Subarray to Sort in Every Sliding Window/README_EN.md b/solution/3500-3599/3555.Smallest Subarray to Sort in Every Sliding Window/README_EN.md new file mode 100644 index 0000000000000..f8a58575d7e24 --- /dev/null +++ b/solution/3500-3599/3555.Smallest Subarray to Sort in Every Sliding Window/README_EN.md @@ -0,0 +1,102 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3555.Smallest%20Subarray%20to%20Sort%20in%20Every%20Sliding%20Window/README_EN.md +--- + + + +# [3555. Smallest Subarray to Sort in Every Sliding Window 🔒](https://leetcode.com/problems/smallest-subarray-to-sort-in-every-sliding-window) + +[中文文档](/solution/3500-3599/3555.Smallest%20Subarray%20to%20Sort%20in%20Every%20Sliding%20Window/README.md) + +## Description + + + +

You are given an integer array nums and an integer k.

+ +

For each contiguous subarray of length k, determine the minimum length of a continuous segment that must be sorted so that the entire window becomes non‑decreasing; if the window is already sorted, its required length is zero.

+ +

Return an array of length n − k + 1 where each element corresponds to the answer for its window.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,3,2,4,5], k = 3

+ +

Output: [2,2,0]

+ +

Explanation:

+ + +
+ +

Example 2:

+ +
+

Input: nums = [5,4,3,2,1], k = 4

+ +

Output: [4,4]

+ +

Explanation:

+ + +
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 8bb1785a384f2..15667df5e5103 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -315,6 +315,7 @@ | 3482 | [分析组织层级](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README.md) | `数据库` | 困难 | | | 3497 | [分析订阅转化](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README.md) | `数据库` | 中等 | | | 3521 | [查找推荐产品对](/solution/3500-3599/3521.Find%20Product%20Recommendation%20Pairs/README.md) | `数据库` | 中等 | | +| 3554 | [查找类别推荐对](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README.md) | `数据库` | 困难 | | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 5ec8e3b4018e1..f8c122fac5ac6 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -313,6 +313,7 @@ Press Control + F(or Command + F on | 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README_EN.md) | `Database` | Hard | | | 3497 | [Analyze Subscription Conversion](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README_EN.md) | `Database` | Medium | | | 3521 | [Find Product Recommendation Pairs](/solution/3500-3599/3521.Find%20Product%20Recommendation%20Pairs/README_EN.md) | `Database` | Medium | | +| 3554 | [Find Category Recommendation Pairs](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README_EN.md) | `Database` | Hard | | ## Copyright diff --git a/solution/README.md b/solution/README.md index d37296a5fde6e..3baeaa53c3b82 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3564,6 +3564,8 @@ | 3551 | [数位和排序需要的最小交换次数](/solution/3500-3599/3551.Minimum%20Swaps%20to%20Sort%20by%20Digit%20Sum/README.md) | | 中等 | 第 450 场周赛 | | 3552 | [网格传送门旅游](/solution/3500-3599/3552.Grid%20Teleportation%20Traversal/README.md) | | 中等 | 第 450 场周赛 | | 3553 | [包含给定路径的最小带权子树 II](/solution/3500-3599/3553.Minimum%20Weighted%20Subgraph%20With%20the%20Required%20Paths%20II/README.md) | | 困难 | 第 450 场周赛 | +| 3554 | [查找类别推荐对](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README.md) | `数据库` | 困难 | | +| 3555 | [排序每个滑动窗口中最小的子数组](/solution/3500-3599/3555.Smallest%20Subarray%20to%20Sort%20in%20Every%20Sliding%20Window/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 3b8936cbd58ac..44f18b19ee47a 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3562,6 +3562,8 @@ Press Control + F(or Command + F on | 3551 | [Minimum Swaps to Sort by Digit Sum](/solution/3500-3599/3551.Minimum%20Swaps%20to%20Sort%20by%20Digit%20Sum/README_EN.md) | | Medium | Weekly Contest 450 | | 3552 | [Grid Teleportation Traversal](/solution/3500-3599/3552.Grid%20Teleportation%20Traversal/README_EN.md) | | Medium | Weekly Contest 450 | | 3553 | [Minimum Weighted Subgraph With the Required Paths II](/solution/3500-3599/3553.Minimum%20Weighted%20Subgraph%20With%20the%20Required%20Paths%20II/README_EN.md) | | Hard | Weekly Contest 450 | +| 3554 | [Find Category Recommendation Pairs](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README_EN.md) | `Database` | Hard | | +| 3555 | [Smallest Subarray to Sort in Every Sliding Window](/solution/3500-3599/3555.Smallest%20Subarray%20to%20Sort%20in%20Every%20Sliding%20Window/README_EN.md) | | Medium | 🔒 | ## Copyright