diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md index 726f4d0dc6c8f..65233444de11f 100644 --- a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md +++ b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README.md +tags: + - 贪心 + - 数组 + - 数学 + - 堆(优先队列) --- @@ -24,9 +29,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Fi
i
个细菌菌株需要 timeReq[i]
个时间单位来被消除。splitTime
单位时间。一旦分裂,两个白细胞就可以 并行 消灭细菌。您必须确定消除所有细菌菌株所需的 最短 时间。
@@ -58,7 +63,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Fi输入:timeReq = [10,4], splitTime = 5
-输出:5
+输出:15
解释:
diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md index f15ef72f3875e..506e72053a5d0 100644 --- a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md +++ b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README_EN.md +tags: + - Greedy + - Array + - Math + - Heap (Priority Queue) --- diff --git a/solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README.md b/solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README.md index bf21ab601b9a8..fd530cf92144e 100644 --- a/solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README.md +++ b/solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README.md @@ -2,6 +2,14 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3507.Minimum%20Pair%20Removal%20to%20Sort%20Array%20I/README.md +tags: + - 数组 + - 哈希表 + - 链表 + - 双向链表 + - 有序集合 + - 模拟 + - 堆(优先队列) --- diff --git a/solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README_EN.md b/solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README_EN.md index 81dee63d6cb4a..37c9eed1351ee 100644 --- a/solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README_EN.md +++ b/solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README_EN.md @@ -2,6 +2,14 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3507.Minimum%20Pair%20Removal%20to%20Sort%20Array%20I/README_EN.md +tags: + - Array + - Hash Table + - Linked List + - Doubly-Linked List + - Ordered Set + - Simulation + - Heap (Priority Queue) --- diff --git a/solution/3500-3599/3508.Implement Router/README.md b/solution/3500-3599/3508.Implement Router/README.md index 20e6dcae91eea..883b13f3bf6ae 100644 --- a/solution/3500-3599/3508.Implement Router/README.md +++ b/solution/3500-3599/3508.Implement Router/README.md @@ -2,6 +2,13 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3508.Implement%20Router/README.md +tags: + - 设计 + - 队列 + - 数组 + - 哈希表 + - 二分查找 + - 有序集合 --- diff --git a/solution/3500-3599/3508.Implement Router/README_EN.md b/solution/3500-3599/3508.Implement Router/README_EN.md index 78aa429a92329..0e00e4556d762 100644 --- a/solution/3500-3599/3508.Implement Router/README_EN.md +++ b/solution/3500-3599/3508.Implement Router/README_EN.md @@ -2,6 +2,13 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3508.Implement%20Router/README_EN.md +tags: + - Design + - Queue + - Array + - Hash Table + - Binary Search + - Ordered Set --- diff --git a/solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README.md b/solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README.md index a6477f2b58385..d31c1cd504931 100644 --- a/solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README.md +++ b/solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3509.Maximum%20Product%20of%20Subsequences%20With%20an%20Alternating%20Sum%20Equal%20to%20K/README.md +tags: + - 数组 + - 哈希表 + - 动态规划 --- diff --git a/solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README_EN.md b/solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README_EN.md index 86a838b97d445..992c08b18c475 100644 --- a/solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README_EN.md +++ b/solution/3500-3599/3509.Maximum Product of Subsequences With an Alternating Sum Equal to K/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3509.Maximum%20Product%20of%20Subsequences%20With%20an%20Alternating%20Sum%20Equal%20to%20K/README_EN.md +tags: + - Array + - Hash Table + - Dynamic Programming --- diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md index 477593cc7a52a..24eaaa5886a18 100644 --- a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md @@ -2,6 +2,9 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Minimum%20Operations%20to%20Make%20Array%20Sum%20Divisible%20by%20K/README.md +tags: + - 数组 + - 数学 --- diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md index e4adb8d98a3e9..6402962ce2ac4 100644 --- a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md @@ -2,6 +2,9 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Minimum%20Operations%20to%20Make%20Array%20Sum%20Divisible%20by%20K/README_EN.md +tags: + - Array + - Math --- diff --git a/solution/3500-3599/3513.Number of Unique XOR Triplets I/README.md b/solution/3500-3599/3513.Number of Unique XOR Triplets I/README.md index 5b28cded68e9b..3c524aa263489 100644 --- a/solution/3500-3599/3513.Number of Unique XOR Triplets I/README.md +++ b/solution/3500-3599/3513.Number of Unique XOR Triplets I/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3513.Number%20of%20Unique%20XOR%20Triplets%20I/README.md +tags: + - 位运算 + - 数组 + - 数学 --- diff --git a/solution/3500-3599/3513.Number of Unique XOR Triplets I/README_EN.md b/solution/3500-3599/3513.Number of Unique XOR Triplets I/README_EN.md index b0b4ae21dd5c9..a53461c41049b 100644 --- a/solution/3500-3599/3513.Number of Unique XOR Triplets I/README_EN.md +++ b/solution/3500-3599/3513.Number of Unique XOR Triplets I/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3513.Number%20of%20Unique%20XOR%20Triplets%20I/README_EN.md +tags: + - Bit Manipulation + - Array + - Math --- diff --git a/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md b/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md index a46859fdf3297..bc023472db15b 100644 --- a/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md +++ b/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3514.Number%20of%20Unique%20XOR%20Triplets%20II/README.md +tags: + - 位运算 + - 数组 + - 数学 + - 枚举 --- diff --git a/solution/3500-3599/3514.Number of Unique XOR Triplets II/README_EN.md b/solution/3500-3599/3514.Number of Unique XOR Triplets II/README_EN.md index 6e0ecb8588db6..4efa733aed262 100644 --- a/solution/3500-3599/3514.Number of Unique XOR Triplets II/README_EN.md +++ b/solution/3500-3599/3514.Number of Unique XOR Triplets II/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3514.Number%20of%20Unique%20XOR%20Triplets%20II/README_EN.md +tags: + - Bit Manipulation + - Array + - Math + - Enumeration --- diff --git a/solution/3500-3599/3515.Shortest Path in a Weighted Tree/README.md b/solution/3500-3599/3515.Shortest Path in a Weighted Tree/README.md index 1f92729019265..1d5abb7ed388b 100644 --- a/solution/3500-3599/3515.Shortest Path in a Weighted Tree/README.md +++ b/solution/3500-3599/3515.Shortest Path in a Weighted Tree/README.md @@ -2,6 +2,12 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3515.Shortest%20Path%20in%20a%20Weighted%20Tree/README.md +tags: + - 树 + - 深度优先搜索 + - 树状数组 + - 线段树 + - 数组 --- diff --git a/solution/3500-3599/3515.Shortest Path in a Weighted Tree/README_EN.md b/solution/3500-3599/3515.Shortest Path in a Weighted Tree/README_EN.md index 61cb5faf270c8..4ee7d80de5e7e 100644 --- a/solution/3500-3599/3515.Shortest Path in a Weighted Tree/README_EN.md +++ b/solution/3500-3599/3515.Shortest Path in a Weighted Tree/README_EN.md @@ -2,6 +2,12 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3515.Shortest%20Path%20in%20a%20Weighted%20Tree/README_EN.md +tags: + - Tree + - Depth-First Search + - Binary Indexed Tree + - Segment Tree + - Array --- diff --git a/solution/3500-3599/3516.Find Closest Person/README.md b/solution/3500-3599/3516.Find Closest Person/README.md index cb8fba908dd59..b71837a7a1e1f 100644 --- a/solution/3500-3599/3516.Find Closest Person/README.md +++ b/solution/3500-3599/3516.Find Closest Person/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3516.Find%20Closest%20Person/README.md +tags: + - 数学 --- diff --git a/solution/3500-3599/3516.Find Closest Person/README_EN.md b/solution/3500-3599/3516.Find Closest Person/README_EN.md index f35e386b31fa2..d283978a8f1e9 100644 --- a/solution/3500-3599/3516.Find Closest Person/README_EN.md +++ b/solution/3500-3599/3516.Find Closest Person/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3516.Find%20Closest%20Person/README_EN.md +tags: + - Math --- diff --git a/solution/3500-3599/3517.Smallest Palindromic Rearrangement I/README.md b/solution/3500-3599/3517.Smallest Palindromic Rearrangement I/README.md index 8640bb78020a9..bb9ecb6c9503d 100644 --- a/solution/3500-3599/3517.Smallest Palindromic Rearrangement I/README.md +++ b/solution/3500-3599/3517.Smallest Palindromic Rearrangement I/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3517.Smallest%20Palindromic%20Rearrangement%20I/README.md +tags: + - 字符串 + - 计数排序 + - 排序 --- diff --git a/solution/3500-3599/3517.Smallest Palindromic Rearrangement I/README_EN.md b/solution/3500-3599/3517.Smallest Palindromic Rearrangement I/README_EN.md index 5819a029babf5..63c46b6a547cb 100644 --- a/solution/3500-3599/3517.Smallest Palindromic Rearrangement I/README_EN.md +++ b/solution/3500-3599/3517.Smallest Palindromic Rearrangement I/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3517.Smallest%20Palindromic%20Rearrangement%20I/README_EN.md +tags: + - String + - Counting Sort + - Sorting --- diff --git a/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md b/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md index ad4f85d277275..39e121fea8b3c 100644 --- a/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md +++ b/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md @@ -2,6 +2,12 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3518.Smallest%20Palindromic%20Rearrangement%20II/README.md +tags: + - 哈希表 + - 数学 + - 字符串 + - 组合数学 + - 计数 --- diff --git a/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README_EN.md b/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README_EN.md index 819f2917f2403..0f53ad76c524b 100644 --- a/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README_EN.md +++ b/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README_EN.md @@ -2,6 +2,12 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3518.Smallest%20Palindromic%20Rearrangement%20II/README_EN.md +tags: + - Hash Table + - Math + - String + - Combinatorics + - Counting --- diff --git a/solution/3500-3599/3519.Count Numbers with Non-Decreasing Digits/README.md b/solution/3500-3599/3519.Count Numbers with Non-Decreasing Digits/README.md index 66bd25fcfd96f..058eb535d1e6c 100644 --- a/solution/3500-3599/3519.Count Numbers with Non-Decreasing Digits/README.md +++ b/solution/3500-3599/3519.Count Numbers with Non-Decreasing Digits/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3519.Count%20Numbers%20with%20Non-Decreasing%20Digits/README.md +tags: + - 数学 + - 字符串 + - 动态规划 --- diff --git a/solution/3500-3599/3519.Count Numbers with Non-Decreasing Digits/README_EN.md b/solution/3500-3599/3519.Count Numbers with Non-Decreasing Digits/README_EN.md index 130e109f581e1..c4faa3d92f11a 100644 --- a/solution/3500-3599/3519.Count Numbers with Non-Decreasing Digits/README_EN.md +++ b/solution/3500-3599/3519.Count Numbers with Non-Decreasing Digits/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3519.Count%20Numbers%20with%20Non-Decreasing%20Digits/README_EN.md +tags: + - Math + - String + - Dynamic Programming --- diff --git a/solution/3500-3599/3520.Minimum Threshold for Inversion Pairs Count/README.md b/solution/3500-3599/3520.Minimum Threshold for Inversion Pairs Count/README.md new file mode 100644 index 0000000000000..34dd950504cfd --- /dev/null +++ b/solution/3500-3599/3520.Minimum Threshold for Inversion Pairs Count/README.md @@ -0,0 +1,129 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3520.Minimum%20Threshold%20for%20Inversion%20Pairs%20Count/README.md +--- + + + +# [3520. 逆序对计数的最小阈值 🔒](https://leetcode.cn/problems/minimum-threshold-for-inversion-pairs-count) + +[English Version](/solution/3500-3599/3520.Minimum%20Threshold%20for%20Inversion%20Pairs%20Count/README_EN.md) + +## 题目描述 + + + +给定一个整数数组 nums
和一个整数 k
。
阈值 为 x
的逆序对是一对下标 (i, j)
满足:
i < j
nums[i] > nums[j]
x
(即 nums[i] - nums[j] <= x
)。你的任务是确定最小的整数 min_threshold
,使得 至少 有 k
个逆序对的阈值是 min_threshold
。
如果没有这样的整数,返回 -1
。
+ +
示例 1:
+ +输入:nums = [1,2,3,4,3,2,1], k = 7
+ +输出:2
+ +解释:
+ +对于阈值 x = 2
,逆序对有:
(3, 4)
其中 nums[3] == 4
和 nums[4] == 3
.(2, 5)
其中 nums[2] == 3
和 nums[5] == 2
.(3, 5)
其中 nums[3] == 4
和 nums[5] == 2
.(4, 5)
其中 nums[4] == 3
和 nums[5] == 2
.(1, 6)
其中 nums[1] == 2
和 nums[6] == 1
.(2, 6)
其中 nums[2] == 3
和 nums[6] == 1
.(4, 6)
其中 nums[4] == 3
和 nums[6] == 1
.(5, 6)
其中 nums[5] == 2
和 nums[6] == 1
.如果我们选择小于 2 的任意整数作为阈值,则逆序对的数量少于 k
。
示例 2:
+ +输入:nums = [10,9,9,9,1], k = 4
+ +输出:8
+ +解释:
+ +对于阈值 x = 8
,逆序对有:
(0, 1)
其中 nums[0] == 10
和 nums[1] == 9
。(0, 2)
其中 nums[0] == 10
和 nums[2] == 9
。(0, 3)
其中 nums[0] == 10
和 nums[3] == 9
。(1, 4)
其中 nums[1] == 9
和 nums[4] == 1
。(2, 4)
其中 nums[2] == 9
和 nums[4] == 1
。(3, 4)
其中 nums[3] == 9
和 nums[4] == 1
。如果我们选择小于 8 的任意整数作为阈值,则逆序对的数量少于 k
。
+ +
提示:
+ +1 <= nums.length <= 104
1 <= nums[i] <= 109
1 <= k <= 109
You are given an array of integers nums
and an integer k
.
An inversion pair with a threshold x
is defined as a pair of indices (i, j)
such that:
i < j
nums[i] > nums[j]
x
(i.e. nums[i] - nums[j] <= x
).Your task is to determine the minimum integer min_threshold
such that there are at least k
inversion pairs with threshold min_threshold
.
If no such integer exists, return -1
.
+
Example 1:
+ +Input: nums = [1,2,3,4,3,2,1], k = 7
+ +Output: 2
+ +Explanation:
+ +For threshold x = 2
, the pairs are:
(3, 4)
where nums[3] == 4
and nums[4] == 3
.(2, 5)
where nums[2] == 3
and nums[5] == 2
.(3, 5)
where nums[3] == 4
and nums[5] == 2
.(4, 5)
where nums[4] == 3
and nums[5] == 2
.(1, 6)
where nums[1] == 2
and nums[6] == 1
.(2, 6)
where nums[2] == 3
and nums[6] == 1
.(4, 6)
where nums[4] == 3
and nums[6] == 1
.(5, 6)
where nums[5] == 2
and nums[6] == 1
.There are less than k
inversion pairs if we choose any integer less than 2 as threshold.
Example 2:
+ +Input: nums = [10,9,9,9,1], k = 4
+ +Output: 8
+ +Explanation:
+ +For threshold x = 8
, the pairs are:
(0, 1)
where nums[0] == 10
and nums[1] == 9
.(0, 2)
where nums[0] == 10
and nums[2] == 9
.(0, 3)
where nums[0] == 10
and nums[3] == 9
.(1, 4)
where nums[1] == 9
and nums[4] == 1
.(2, 4)
where nums[2] == 9
and nums[4] == 1
.(3, 4)
where nums[3] == 9
and nums[4] == 1
.There are less than k
inversion pairs if we choose any integer less than 8 as threshold.
+
Constraints:
+ +1 <= nums.length <= 104
1 <= nums[i] <= 109
1 <= k <= 109
表: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 是这张表的唯一主键。 +每一行表示一个产品的类别和价格。 ++ +
亚马逊希望根据 共同购买模式 实现 “购买此商品的用户还购买了...” 功能。编写一个解决方案以实现:
+ +product1_id
< product2_id
)如果 至少有 3
位不同的 客户同时购买了这两种产品,则认为该 产品对 适合推荐。
返回结果表以 customer_count 降序 排序,并且为了避免排序持平,以 product1_id
升序 排序,并以 product2_id
升序 排序。
结果格式如下所示。
+ ++ +
示例:
+ +输入:
+ +ProductPurchases 表:
+ +++---------+------------+----------+ +| user_id | product_id | quantity | ++---------+------------+----------+ +| 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 表:
+ +++------------+-------------+-------+ +| product_id | category | price | ++------------+-------------+-------+ +| 101 | Electronics | 100 | +| 102 | Books | 20 | +| 103 | Clothing | 35 | +| 104 | Kitchen | 50 | +| 105 | Sports | 75 | ++------------+-------------+-------+ ++ +
输出:
+ +++-------------+-------------+-------------------+-------------------+----------------+ +| product1_id | product2_id | product1_category | product2_category | customer_count | ++-------------+-------------+-------------------+-------------------+----------------+ +| 101 | 102 | Electronics | Books | 3 | +| 101 | 103 | Electronics | Clothing | 3 | +| 102 | 104 | Books | Kitchen | 3 | ++-------------+-------------+-------------------+-------------------+----------------+ ++ +
解释:
+ +结果以 customer_count 降序排序。对于有相同 customer_count 的产品对,将它们以 product1_id 升序排序,然后以 product2_id 升序排序。
+Table: ProductPurchases
++-------------+------+ +| Column Name | Type | ++-------------+------+ +| user_id | int | +| product_id | int | +| quantity | int | ++-------------+------+ +(user_id, product_id) is the unique key 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 primary key for this table. +Each row assigns a category and price to a product. ++ +
Amazon wants to implement the Customers who bought this also bought... feature based on co-purchase patterns. Write a solution to :
+ +product1_id
< product2_id
)A product pair is considered for recommendation if at least 3
different customers have purchased both products.
Return the result table ordered by customer_count in descending order, and in case of a tie, by product1_id
in ascending order, and then by product2_id
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 | 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 table:
+ +++------------+-------------+-------+ +| product_id | category | price | ++------------+-------------+-------+ +| 101 | Electronics | 100 | +| 102 | Books | 20 | +| 103 | Clothing | 35 | +| 104 | Kitchen | 50 | +| 105 | Sports | 75 | ++------------+-------------+-------+ ++ +
Output:
+ +++-------------+-------------+-------------------+-------------------+----------------+ +| product1_id | product2_id | product1_category | product2_category | customer_count | ++-------------+-------------+-------------------+-------------------+----------------+ +| 101 | 102 | Electronics | Books | 3 | +| 101 | 103 | Electronics | Clothing | 3 | +| 102 | 104 | Books | Kitchen | 3 | ++-------------+-------------+-------------------+-------------------+----------------+ ++ +
Explanation:
+ +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.
+