Skip to content

Commit ff2e8d5

Browse files
authored
feat: add sql solution to lc problem: No.2922 (#1918)
No.2922.Market Analysis III
1 parent 8564bbe commit ff2e8d5

File tree

13 files changed

+473
-0
lines changed

13 files changed

+473
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [2921. Maximum Profitable Triplets With Increasing Prices II](https://leetcode.cn/problems/maximum-profitable-triplets-with-increasing-prices-ii)
2+
3+
[English Version](/solution/2900-2999/2921.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20II/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Given the <strong>0-indexed</strong> arrays <code>prices</code> and <code>profits</code> of length <code>n</code>. There are <code>n</code> items in an store where the <code>i<sup>th</sup></code> item has a price of <code>prices[i]</code> and a profit of <code>profits[i]</code>.</p>
10+
11+
<p>We have to pick three items with the following condition:</p>
12+
13+
<ul>
14+
<li><code>prices[i] &lt; prices[j] &lt; prices[k]</code> where <code>i &lt; j &lt; k</code>.</li>
15+
</ul>
16+
17+
<p>If we pick items with indices <code>i</code>, <code>j</code> and <code>k</code> satisfying the above condition, the profit would be <code>profits[i] + profits[j] + profits[k]</code>.</p>
18+
19+
<p>Return<em> the <strong>maximum profit</strong> we can get, and </em><code>-1</code><em> if it&#39;s not possible to pick three items with the given condition.</em></p>
20+
21+
<p>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> prices = [10,2,3,4], profits = [100,2,7,10]
26+
<strong>Output:</strong> 19
27+
<strong>Explanation:</strong> We can&#39;t pick the item with index i=0 since there are no indices j and k such that the condition holds.
28+
So the only triplet we can pick, are the items with indices 1, 2 and 3 and it&#39;s a valid pick since prices[1] &lt; prices[2] &lt; prices[3].
29+
The answer would be sum of their profits which is 2 + 7 + 10 = 19.</pre>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> prices = [1,2,3,4,5], profits = [1,5,3,4,6]
35+
<strong>Output:</strong> 15
36+
<strong>Explanation:</strong> We can select any triplet of items since for each triplet of indices i, j and k such that i &lt; j &lt; k, the condition holds.
37+
Therefore the maximum profit we can get would be the 3 most profitable items which are indices 1, 3 and 4.
38+
The answer would be sum of their profits which is 5 + 4 + 6 = 15.</pre>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> prices = [4,3,2,1], profits = [33,20,19,87]
44+
<strong>Output:</strong> -1
45+
<strong>Explanation:</strong> We can&#39;t select any triplet of indices such that the condition holds, so we return -1.
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>3 &lt;= prices.length == profits.length &lt;= 50000</code></li>
53+
<li><code>1 &lt;= prices[i] &lt;= 5000</code></li>
54+
<li><code>1 &lt;= profits[i] &lt;= 10<sup>6</sup></code></li>
55+
</ul>
56+
57+
## 解法
58+
59+
<!-- 这里可写通用的实现逻辑 -->
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```python
68+
69+
```
70+
71+
### **Java**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```java
76+
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
83+
```
84+
85+
### **Go**
86+
87+
```go
88+
89+
```
90+
91+
### **...**
92+
93+
```
94+
95+
```
96+
97+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [2921. Maximum Profitable Triplets With Increasing Prices II](https://leetcode.com/problems/maximum-profitable-triplets-with-increasing-prices-ii)
2+
3+
[中文文档](/solution/2900-2999/2921.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20II/README.md)
4+
5+
## Description
6+
7+
<p>Given the <strong>0-indexed</strong> arrays <code>prices</code> and <code>profits</code> of length <code>n</code>. There are <code>n</code> items in an store where the <code>i<sup>th</sup></code> item has a price of <code>prices[i]</code> and a profit of <code>profits[i]</code>.</p>
8+
9+
<p>We have to pick three items with the following condition:</p>
10+
11+
<ul>
12+
<li><code>prices[i] &lt; prices[j] &lt; prices[k]</code> where <code>i &lt; j &lt; k</code>.</li>
13+
</ul>
14+
15+
<p>If we pick items with indices <code>i</code>, <code>j</code> and <code>k</code> satisfying the above condition, the profit would be <code>profits[i] + profits[j] + profits[k]</code>.</p>
16+
17+
<p>Return<em> the <strong>maximum profit</strong> we can get, and </em><code>-1</code><em> if it&#39;s not possible to pick three items with the given condition.</em></p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> prices = [10,2,3,4], profits = [100,2,7,10]
24+
<strong>Output:</strong> 19
25+
<strong>Explanation:</strong> We can&#39;t pick the item with index i=0 since there are no indices j and k such that the condition holds.
26+
So the only triplet we can pick, are the items with indices 1, 2 and 3 and it&#39;s a valid pick since prices[1] &lt; prices[2] &lt; prices[3].
27+
The answer would be sum of their profits which is 2 + 7 + 10 = 19.</pre>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> prices = [1,2,3,4,5], profits = [1,5,3,4,6]
33+
<strong>Output:</strong> 15
34+
<strong>Explanation:</strong> We can select any triplet of items since for each triplet of indices i, j and k such that i &lt; j &lt; k, the condition holds.
35+
Therefore the maximum profit we can get would be the 3 most profitable items which are indices 1, 3 and 4.
36+
The answer would be sum of their profits which is 5 + 4 + 6 = 15.</pre>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> prices = [4,3,2,1], profits = [33,20,19,87]
42+
<strong>Output:</strong> -1
43+
<strong>Explanation:</strong> We can&#39;t select any triplet of indices such that the condition holds, so we return -1.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>3 &lt;= prices.length == profits.length &lt;= 50000</code></li>
51+
<li><code>1 &lt;= prices[i] &lt;= 5000</code></li>
52+
<li><code>1 &lt;= profits[i] &lt;= 10<sup>6</sup></code></li>
53+
</ul>
54+
55+
## Solutions
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
```python
62+
63+
```
64+
65+
### **Java**
66+
67+
```java
68+
69+
```
70+
71+
### **C++**
72+
73+
```cpp
74+
75+
```
76+
77+
### **Go**
78+
79+
```go
80+
81+
```
82+
83+
### **...**
84+
85+
```
86+
87+
```
88+
89+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# [2922. Market Analysis III](https://leetcode.cn/problems/market-analysis-iii)
2+
3+
[English Version](/solution/2900-2999/2922.Market%20Analysis%20III/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Users</code></p>
10+
11+
<pre>
12+
+----------------+---------+
13+
| Column Name | Type |
14+
+----------------+---------+
15+
| seller_id | int |
16+
| join_date | date |
17+
| favorite_brand | varchar |
18+
+----------------+---------+
19+
seller_id is the primary key for this table.
20+
This table contains seller id, join date, and favorite brand of sellers.
21+
</pre>
22+
23+
<p>Table: <code>Items</code></p>
24+
25+
<pre>
26+
+---------------+---------+
27+
| Column Name | Type |
28+
+---------------+---------+
29+
| item_id | int |
30+
| item_brand | varchar |
31+
+---------------+---------+
32+
item_id is the primary key for this table.
33+
This table contains item id and item brand.</pre>
34+
35+
<p>Table: <code>Orders</code></p>
36+
37+
<pre>
38+
+---------------+---------+
39+
| Column Name | Type |
40+
+---------------+---------+
41+
| order_id | int |
42+
| order_date | date |
43+
| item_id | int |
44+
| seller_id | int |
45+
+---------------+---------+
46+
order_id is the primary key for this table.
47+
item_id is a foreign key to the Items table.
48+
seller_id is a foreign key to the Users table.
49+
This table contains order id, order date, item id and seller id.</pre>
50+
51+
<p>Write a solution to find the <strong>top seller</strong> who has sold the highest number of<strong> unique</strong> items with a <strong>different</strong> brand than their favorite brand. If there are multiple sellers with the same highest count, return all of them.</p>
52+
53+
<p>Return <em>the result table ordered by</em> <code>seller_id</code> <em>in <strong>ascending</strong> order.</em></p>
54+
55+
<p>The result format is in the following example.</p>
56+
57+
<p>&nbsp;</p>
58+
<p><strong class="example">Example 1:</strong></p>
59+
60+
<pre>
61+
<strong>Input:</strong>
62+
Users table:
63+
+-----------+------------+----------------+
64+
| seller_id | join_date | favorite_brand |
65+
+-----------+------------+----------------+
66+
| 1 | 2019-01-01 | Lenovo |
67+
| 2 | 2019-02-09 | Samsung |
68+
| 3 | 2019-01-19 | LG |
69+
+-----------+------------+----------------+
70+
Orders table:
71+
+----------+------------+---------+-----------+
72+
| order_id | order_date | item_id | seller_id |
73+
+----------+------------+---------+-----------+
74+
| 1 | 2019-08-01 | 4 | 2 |
75+
| 2 | 2019-08-02 | 2 | 3 |
76+
| 3 | 2019-08-03 | 3 | 3 |
77+
| 4 | 2019-08-04 | 1 | 2 |
78+
| 5 | 2019-08-04 | 4 | 2 |
79+
+----------+------------+---------+-----------+
80+
Items table:
81+
+---------+------------+
82+
| item_id | item_brand |
83+
+---------+------------+
84+
| 1 | Samsung |
85+
| 2 | Lenovo |
86+
| 3 | LG |
87+
| 4 | HP |
88+
+---------+------------+
89+
<strong>Output:</strong>
90+
+-----------+-----------+
91+
| seller_id | num_items |
92+
+-----------+-----------+
93+
| 2 | 1 |
94+
| 3 | 1 |
95+
+-----------+-----------+
96+
<strong>Explanation:</strong>
97+
- The user with seller_id 2 has sold three items, but only two of them are not marked as a favorite. We will include a unique count of 1 because both of these items are identical.
98+
- The user with seller_id 3 has sold two items, but only one of them is not marked as a favorite. We will include just that non-favorite item in our count.
99+
Since seller_ids 2 and 3 have the same count of one item each, they both will be displayed in the output.</pre>
100+
101+
## 解法
102+
103+
<!-- 这里可写通用的实现逻辑 -->
104+
105+
**方法一:等值连接 + 分组 + 子查询**
106+
107+
我们可以使用等值连接,将 `Orders` 表和 `Users` 表按照 `seller_id` 进行连接,接着再按照 `item_id` 连接 `Items`,筛选出 `item_brand` 不等于 `favorite_brand` 的记录,然后按照 `seller_id` 进行分组,统计每个 `seller_id` 对应的 `item_id` 的个数,最后再使用子查询,找出 `item_id` 个数最多的 `seller_id`
108+
109+
<!-- tabs:start -->
110+
111+
### **SQL**
112+
113+
<!-- 这里可写当前语言的特殊实现逻辑 -->
114+
115+
```sql
116+
# Write your MySQL query statement below
117+
WITH
118+
T AS (
119+
SELECT seller_id, COUNT(DISTINCT item_id) AS num_items
120+
FROM
121+
Orders
122+
JOIN Users USING (seller_id)
123+
JOIN Items USING (item_id)
124+
WHERE item_brand != favorite_brand
125+
GROUP BY 1
126+
)
127+
SELECT seller_id, num_items
128+
FROM T
129+
WHERE num_items = (SELECT MAX(num_items) FROM T)
130+
ORDER BY 1;
131+
```
132+
133+
<!-- tabs:end -->

0 commit comments

Comments
 (0)