Skip to content

Commit 793ac0a

Browse files
committed
Added task 3554
1 parent 24f92f0 commit 793ac0a

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,7 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3554 |[Find Category Recommendation Pairs](src/main/kotlin/g3501_3600/s3554_find_category_recommendation_pairs)| Hard | Database | 623 | 82.76
20912092
| 3553 |[Minimum Weighted Subgraph With the Required Paths II](src/main/kotlin/g3501_3600/s3553_minimum_weighted_subgraph_with_the_required_paths_ii)| Hard | Array, Depth_First_Search, Tree | 142 | 100.00
20922093
| 3552 |[Grid Teleportation Traversal](src/main/kotlin/g3501_3600/s3552_grid_teleportation_traversal)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix | 147 | 100.00
20932094
| 3551 |[Minimum Swaps to Sort by Digit Sum](src/main/kotlin/g3501_3600/s3551_minimum_swaps_to_sort_by_digit_sum)| Medium | Array, Hash_Table, Sorting | 481 | 83.33
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3554\. Find Category Recommendation Pairs
5+
6+
Table: `ProductPurchases`
7+
8+
+-------------+------+
9+
| Column Name | Type |
10+
+-------------+------+
11+
| user_id | int |
12+
| product_id | int |
13+
| quantity | int |
14+
+-------------+------+
15+
(user_id, product_id) is the unique identifier for this table.
16+
Each row represents a purchase of a product by a user in a specific quantity.
17+
18+
Table: `ProductInfo`
19+
20+
+-------------+---------+
21+
| Column Name | Type |
22+
+-------------+---------+
23+
| product_id | int |
24+
| category | varchar |
25+
| price | decimal |
26+
+-------------+---------+
27+
product_id is the unique identifier for this table.
28+
Each row assigns a category and price to a product.
29+
30+
Amazon wants to understand shopping patterns across product categories. Write a solution to:
31+
32+
1. Find all **category pairs** (where `category1` < `category2`)
33+
2. For **each category pair**, determine the number of **unique** **customers** who purchased products from **both** categories
34+
35+
A category pair is considered **reportable** if at least `3` different customers have purchased products from both categories.
36+
37+
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._
38+
39+
The result format is in the following example.
40+
41+
**Example:**
42+
43+
**Input:**
44+
45+
ProductPurchases table:
46+
47+
+---------+------------+----------+
48+
| user_id | product_id | quantity |
49+
+---------+------------+----------+
50+
| 1 | 101 | 2 |
51+
| 1 | 102 | 1 |
52+
| 1 | 201 | 3 |
53+
| 1 | 301 | 1 |
54+
| 2 | 101 | 1 |
55+
| 2 | 102 | 2 |
56+
| 2 | 103 | 1 |
57+
| 2 | 201 | 5 |
58+
| 3 | 101 | 2 |
59+
| 3 | 103 | 1 |
60+
| 3 | 301 | 4 |
61+
| 3 | 401 | 2 |
62+
| 4 | 101 | 1 |
63+
| 4 | 201 | 3 |
64+
| 4 | 301 | 1 |
65+
| 4 | 401 | 2 |
66+
| 5 | 102 | 2 |
67+
| 5 | 103 | 1 |
68+
| 5 | 201 | 2 |
69+
| 5 | 202 | 3 |
70+
+---------+------------+----------+
71+
72+
ProductInfo table:
73+
74+
+------------+-------------+-------+
75+
| product_id | category | price |
76+
+------------+-------------+-------+
77+
| 101 | Electronics | 100 |
78+
| 102 | Books | 20 |
79+
| 103 | Books | 35 |
80+
| 201 | Clothing | 45 |
81+
| 202 | Clothing | 60 |
82+
| 301 | Sports | 75 |
83+
| 401 | Kitchen | 50 |
84+
+------------+-------------+-------+
85+
86+
**Output:**
87+
88+
+-------------+-------------+----------------+
89+
| category1 | category2 | customer_count |
90+
+-------------+-------------+----------------+
91+
| Books | Clothing | 3 |
92+
| Books | Electronics | 3 |
93+
| Clothing | Electronics | 3 |
94+
| Electronics | Sports | 3 |
95+
+-------------+-------------+----------------+
96+
97+
**Explanation:**
98+
99+
* **Books-Clothing**:
100+
* User 1 purchased products from Books (102) and Clothing (201)
101+
* User 2 purchased products from Books (102, 103) and Clothing (201)
102+
* User 5 purchased products from Books (102, 103) and Clothing (201, 202)
103+
* Total: 3 customers purchased from both categories
104+
* **Books-Electronics**:
105+
* User 1 purchased products from Books (102) and Electronics (101)
106+
* User 2 purchased products from Books (102, 103) and Electronics (101)
107+
* User 3 purchased products from Books (103) and Electronics (101)
108+
* Total: 3 customers purchased from both categories
109+
* **Clothing-Electronics**:
110+
* User 1 purchased products from Clothing (201) and Electronics (101)
111+
* User 2 purchased products from Clothing (201) and Electronics (101)
112+
* User 4 purchased products from Clothing (201) and Electronics (101)
113+
* Total: 3 customers purchased from both categories
114+
* **Electronics-Sports**:
115+
* User 1 purchased products from Electronics (101) and Sports (301)
116+
* User 3 purchased products from Electronics (101) and Sports (301)
117+
* User 4 purchased products from Electronics (101) and Sports (301)
118+
* Total: 3 customers purchased from both categories
119+
* 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.
120+
121+
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.
122+
123+
## Solution
124+
125+
```sql
126+
# Write your MySQL query statement below
127+
SELECT
128+
pi1.category AS category1,
129+
pi2.category AS category2,
130+
COUNT(DISTINCT pp1.user_id) AS customer_count
131+
FROM
132+
ProductPurchases pp1,
133+
ProductPurchases pp2,
134+
ProductInfo pi1,
135+
ProductInfo pi2
136+
WHERE
137+
pp1.user_id = pp2.user_id
138+
AND pi1.category < pi2.category
139+
AND pp1.product_id = pi1.product_id
140+
AND pp2.product_id = pi2.product_id
141+
GROUP BY
142+
pi1.category,
143+
pi2.category
144+
HAVING
145+
COUNT(DISTINCT pp1.user_id) >= 3
146+
ORDER BY
147+
customer_count DESC,
148+
category1 ASC,
149+
category2 ASC;
150+
```

0 commit comments

Comments
 (0)