Skip to content

Commit e838c31

Browse files
committed
Added task 3554
1 parent 73a2246 commit e838c31

File tree

10 files changed

+225
-32
lines changed

10 files changed

+225
-32
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/java/g3501_3600/s3554_find_category_recommendation_pairs)| Hard | Database | 623 | 82.76
20912092
| 3553 |[Minimum Weighted Subgraph With the Required Paths II](src/main/java/g3501_3600/s3553_minimum_weighted_subgraph_with_the_required_paths_ii)| Hard | Array, Depth_First_Search, Tree | 65 | 100.00
20922093
| 3552 |[Grid Teleportation Traversal](src/main/java/g3501_3600/s3552_grid_teleportation_traversal)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix | 146 | 98.62
20932094
| 3551 |[Minimum Swaps to Sort by Digit Sum](src/main/java/g3501_3600/s3551_minimum_swaps_to_sort_by_digit_sum)| Medium | Array, Hash_Table, Sorting | 213 | 99.23

src/main/java/g0101_0200/s0175_combine_two_tables/readme.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ There is no address in the address table for the personId = 1 so we return null
7777

7878
```sql
7979
# Write your MySQL query statement below
80-
SELECT FirstName, LastName, City, State
81-
FROM Person LEFT JOIN Address USING (PersonId)
80+
SELECT
81+
FirstName,
82+
LastName,
83+
City,
84+
State
85+
FROM
86+
Person
87+
LEFT JOIN
88+
Address
89+
USING (PersonId);
8290
```

src/main/java/g0101_0200/s0176_second_highest_salary/readme.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ The query result format is in the following example.
6666

6767
```sql
6868
# Write your MySQL query statement below
69-
SELECT ifnull(
70-
(SELECT distinct(Salary)
71-
FROM Employee
72-
ORDER BY Salary DESC
73-
LIMIT 1
74-
OFFSET 1), NULL) SecondHighestSalary;
69+
SELECT
70+
IFNULL(
71+
(
72+
SELECT DISTINCT Salary
73+
FROM Employee
74+
ORDER BY Salary DESC
75+
LIMIT 1 OFFSET 1
76+
),
77+
NULL
78+
) AS SecondHighestSalary;
7579
```

src/main/java/g0101_0200/s0178_rank_scores/readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,11 @@ The query result format is in the following example.
6161

6262
```sql
6363
# Write your MySQL query statement below
64-
select Score, DENSE_RANK() OVER(order by Score Desc) as "Rank" from Scores order by "Rank" Asc;
64+
SELECT
65+
Score,
66+
DENSE_RANK() OVER (ORDER BY Score DESC) AS Rank
67+
FROM
68+
Scores
69+
ORDER BY
70+
Rank ASC;
6571
```

src/main/java/g0101_0200/s0180_consecutive_numbers/readme.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ The query result format is in the following example.
5454

5555
```sql
5656
# Write your MySQL query statement below
57-
SELECT DISTINCT l1.num AS ConsecutiveNums
58-
FROM Logs l1
59-
JOIN Logs l2 ON l1.id = l2.id - 1
60-
JOIN Logs l3 ON l1.id = l3.id - 2
61-
WHERE l1.num = l2.num AND l2.num = l3.num;
57+
SELECT DISTINCT
58+
l1.num AS ConsecutiveNums
59+
FROM
60+
Logs l1
61+
JOIN Logs l2 ON l1.id = l2.id - 1
62+
JOIN Logs l3 ON l1.id = l3.id - 2
63+
WHERE
64+
l1.num = l2.num
65+
AND l2.num = l3.num;
6266
```

src/main/java/g0101_0200/s0181_employees_earning_more_than_their_managers/readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ The query result format is in the following example.
5454

5555
```sql
5656
# Write your MySQL query statement below
57-
select a.Name as Employee from Employee a left join Employee b on a.ManagerId=b.Id
58-
where a.Salary > b.Salary and a.ManagerId is not null
57+
SELECT
58+
a.Name AS Employee
59+
FROM
60+
Employee a
61+
LEFT JOIN Employee b ON a.ManagerId = b.Id
62+
WHERE
63+
a.Salary > b.Salary
64+
AND a.ManagerId IS NOT NULL;
5965
```

src/main/java/g0101_0200/s0182_duplicate_emails/readme.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,12 @@ The query result format is in the following example.
5151

5252
```sql
5353
# Write your MySQL query statement below
54-
SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email) > 1;
54+
SELECT
55+
Email
56+
FROM
57+
Person
58+
GROUP BY
59+
Email
60+
HAVING
61+
COUNT(Email) > 1;
5562
```

src/main/java/g0101_0200/s0183_customers_who_never_order/readme.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ The query result format is in the following example.
7171

7272
```sql
7373
# Write your MySQL query statement below
74-
SELECT c.Name as Customers
75-
FROM Customers as c
76-
LEFT JOIN Orders as o
77-
ON c.Id = o.CustomerId
78-
WHERE o.CustomerId is null
74+
SELECT
75+
c.Name AS Customers
76+
FROM
77+
Customers AS c
78+
LEFT JOIN Orders AS o ON c.Id = o.CustomerId
79+
WHERE
80+
o.CustomerId IS NULL;
7981
```

src/main/java/g0101_0200/s0184_department_highest_salary/readme.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,19 @@ SELECT
8282
Sel.Name AS Employee,
8383
Sel.Salary AS Salary
8484
FROM
85-
(
86-
SELECT
87-
Name,
88-
Salary,
89-
DepartmentId,
90-
DENSE_RANK() OVER (PARTITION BY DepartmentId ORDER BY Salary DESC) AS dr
91-
FROM Employee
92-
) AS Sel
93-
INNER JOIN Department d ON d.Id = Sel.DepartmentId
94-
WHERE Sel.dr = 1
85+
(
86+
SELECT
87+
Name,
88+
Salary,
89+
DepartmentId,
90+
DENSE_RANK() OVER (
91+
PARTITION BY DepartmentId
92+
ORDER BY Salary DESC
93+
) AS dr
94+
FROM
95+
Employee
96+
) AS Sel
97+
INNER JOIN Department d ON d.Id = Sel.DepartmentId
98+
WHERE
99+
Sel.dr = 1;
95100
```
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-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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)