|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Hard |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README_EN.md |
| 5 | +tags: |
| 6 | + - Database |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- problem:start --> |
| 10 | + |
| 11 | +# [3554. Find Category Recommendation Pairs](https://leetcode.com/problems/find-category-recommendation-pairs) |
| 12 | + |
| 13 | +[中文文档](/solution/3500-3599/3554.Find%20Category%20Recommendation%20Pairs/README.md) |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Table: <code>ProductPurchases</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++-------------+------+ |
| 23 | +| Column Name | Type | |
| 24 | ++-------------+------+ |
| 25 | +| user_id | int | |
| 26 | +| product_id | int | |
| 27 | +| quantity | int | |
| 28 | ++-------------+------+ |
| 29 | +(user_id, product_id) is the unique identifier for this table. |
| 30 | +Each row represents a purchase of a product by a user in a specific quantity. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p>Table: <code>ProductInfo</code></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | ++-------------+---------+ |
| 37 | +| Column Name | Type | |
| 38 | ++-------------+---------+ |
| 39 | +| product_id | int | |
| 40 | +| category | varchar | |
| 41 | +| price | decimal | |
| 42 | ++-------------+---------+ |
| 43 | +product_id is the unique identifier for this table. |
| 44 | +Each row assigns a category and price to a product. |
| 45 | +</pre> |
| 46 | + |
| 47 | +<p>Amazon wants to understand shopping patterns across product categories. Write a solution to:</p> |
| 48 | + |
| 49 | +<ol> |
| 50 | + <li>Find all <strong>category pairs</strong> (where <code>category1</code> < <code>category2</code>)</li> |
| 51 | + <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> |
| 52 | +</ol> |
| 53 | + |
| 54 | +<p>A category pair is considered <strong>reportable</strong> if at least <code>3</code> different customers have purchased products from both categories.</p> |
| 55 | + |
| 56 | +<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> |
| 57 | + |
| 58 | +<p>The result format is in the following example.</p> |
| 59 | + |
| 60 | +<p> </p> |
| 61 | +<p><strong class="example">Example:</strong></p> |
| 62 | + |
| 63 | +<div class="example-block"> |
| 64 | +<p><strong>Input:</strong></p> |
| 65 | + |
| 66 | +<p>ProductPurchases table:</p> |
| 67 | + |
| 68 | +<pre class="example-io"> |
| 69 | ++---------+------------+----------+ |
| 70 | +| user_id | product_id | quantity | |
| 71 | ++---------+------------+----------+ |
| 72 | +| 1 | 101 | 2 | |
| 73 | +| 1 | 102 | 1 | |
| 74 | +| 1 | 201 | 3 | |
| 75 | +| 1 | 301 | 1 | |
| 76 | +| 2 | 101 | 1 | |
| 77 | +| 2 | 102 | 2 | |
| 78 | +| 2 | 103 | 1 | |
| 79 | +| 2 | 201 | 5 | |
| 80 | +| 3 | 101 | 2 | |
| 81 | +| 3 | 103 | 1 | |
| 82 | +| 3 | 301 | 4 | |
| 83 | +| 3 | 401 | 2 | |
| 84 | +| 4 | 101 | 1 | |
| 85 | +| 4 | 201 | 3 | |
| 86 | +| 4 | 301 | 1 | |
| 87 | +| 4 | 401 | 2 | |
| 88 | +| 5 | 102 | 2 | |
| 89 | +| 5 | 103 | 1 | |
| 90 | +| 5 | 201 | 2 | |
| 91 | +| 5 | 202 | 3 | |
| 92 | ++---------+------------+----------+ |
| 93 | +</pre> |
| 94 | + |
| 95 | +<p>ProductInfo table:</p> |
| 96 | + |
| 97 | +<pre class="example-io"> |
| 98 | ++------------+-------------+-------+ |
| 99 | +| product_id | category | price | |
| 100 | ++------------+-------------+-------+ |
| 101 | +| 101 | Electronics | 100 | |
| 102 | +| 102 | Books | 20 | |
| 103 | +| 103 | Books | 35 | |
| 104 | +| 201 | Clothing | 45 | |
| 105 | +| 202 | Clothing | 60 | |
| 106 | +| 301 | Sports | 75 | |
| 107 | +| 401 | Kitchen | 50 | |
| 108 | ++------------+-------------+-------+ |
| 109 | +</pre> |
| 110 | + |
| 111 | +<p><strong>Output:</strong></p> |
| 112 | + |
| 113 | +<pre class="example-io"> |
| 114 | ++-------------+-------------+----------------+ |
| 115 | +| category1 | category2 | customer_count | |
| 116 | ++-------------+-------------+----------------+ |
| 117 | +| Books | Clothing | 3 | |
| 118 | +| Books | Electronics | 3 | |
| 119 | +| Clothing | Electronics | 3 | |
| 120 | +| Electronics | Sports | 3 | |
| 121 | ++-------------+-------------+----------------+ |
| 122 | +</pre> |
| 123 | + |
| 124 | +<p><strong>Explanation:</strong></p> |
| 125 | + |
| 126 | +<ul> |
| 127 | + <li><strong>Books-Clothing</strong>: |
| 128 | + |
| 129 | + <ul> |
| 130 | + <li>User 1 purchased products from Books (102) and Clothing (201)</li> |
| 131 | + <li>User 2 purchased products from Books (102, 103) and Clothing (201)</li> |
| 132 | + <li>User 5 purchased products from Books (102, 103) and Clothing (201, 202)</li> |
| 133 | + <li>Total: 3 customers purchased from both categories</li> |
| 134 | + </ul> |
| 135 | + </li> |
| 136 | + <li><strong>Books-Electronics</strong>: |
| 137 | + <ul> |
| 138 | + <li>User 1 purchased products from Books (102) and Electronics (101)</li> |
| 139 | + <li>User 2 purchased products from Books (102, 103) and Electronics (101)</li> |
| 140 | + <li>User 3 purchased products from Books (103) and Electronics (101)</li> |
| 141 | + <li>Total: 3 customers purchased from both categories</li> |
| 142 | + </ul> |
| 143 | + </li> |
| 144 | + <li><strong>Clothing-Electronics</strong>: |
| 145 | + <ul> |
| 146 | + <li>User 1 purchased products from Clothing (201) and Electronics (101)</li> |
| 147 | + <li>User 2 purchased products from Clothing (201) and Electronics (101)</li> |
| 148 | + <li>User 4 purchased products from Clothing (201) and Electronics (101)</li> |
| 149 | + <li>Total: 3 customers purchased from both categories</li> |
| 150 | + </ul> |
| 151 | + </li> |
| 152 | + <li><strong>Electronics-Sports</strong>: |
| 153 | + <ul> |
| 154 | + <li>User 1 purchased products from Electronics (101) and Sports (301)</li> |
| 155 | + <li>User 3 purchased products from Electronics (101) and Sports (301)</li> |
| 156 | + <li>User 4 purchased products from Electronics (101) and Sports (301)</li> |
| 157 | + <li>Total: 3 customers purchased from both categories</li> |
| 158 | + </ul> |
| 159 | + </li> |
| 160 | + <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> |
| 161 | + |
| 162 | +</ul> |
| 163 | + |
| 164 | +<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> |
| 165 | +</div> |
| 166 | + |
| 167 | +<!-- description:end --> |
| 168 | + |
| 169 | +## Solutions |
| 170 | + |
| 171 | +<!-- solution:start --> |
| 172 | + |
| 173 | +### Solution 1 |
| 174 | + |
| 175 | +<!-- tabs:start --> |
| 176 | + |
| 177 | +#### MySQL |
| 178 | + |
| 179 | +```sql |
| 180 | + |
| 181 | +``` |
| 182 | + |
| 183 | +<!-- tabs:end --> |
| 184 | + |
| 185 | +<!-- solution:end --> |
| 186 | + |
| 187 | +<!-- problem:end --> |
0 commit comments