diff --git a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README.md b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README.md new file mode 100644 index 0000000000000..d5ed2173b5e42 --- /dev/null +++ b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README.md @@ -0,0 +1,97 @@ +# [2921. Maximum Profitable Triplets With Increasing Prices II](https://leetcode.cn/problems/maximum-profitable-triplets-with-increasing-prices-ii) + +[English Version](/solution/2900-2999/2921.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20II/README_EN.md) + +## 题目描述 + + + +

Given the 0-indexed arrays prices and profits of length n. There are n items in an store where the ith item has a price of prices[i] and a profit of profits[i].

+ +

We have to pick three items with the following condition:

+ + + +

If we pick items with indices i, j and k satisfying the above condition, the profit would be profits[i] + profits[j] + profits[k].

+ +

Return the maximum profit we can get, and -1 if it's not possible to pick three items with the given condition.

+ +

 

+

Example 1:

+ +
+Input: prices = [10,2,3,4], profits = [100,2,7,10]
+Output: 19
+Explanation: We can't pick the item with index i=0 since there are no indices j and k such that the condition holds.
+So the only triplet we can pick, are the items with indices 1, 2 and 3 and it's a valid pick since prices[1] < prices[2] < prices[3].
+The answer would be sum of their profits which is 2 + 7 + 10 = 19.
+ +

Example 2:

+ +
+Input: prices = [1,2,3,4,5], profits = [1,5,3,4,6]
+Output: 15
+Explanation: We can select any triplet of items since for each triplet of indices i, j and k such that i < j < k, the condition holds.
+Therefore the maximum profit we can get would be the 3 most profitable items which are indices 1, 3 and 4.
+The answer would be sum of their profits which is 5 + 4 + 6 = 15.
+ +

Example 3:

+ +
+Input: prices = [4,3,2,1], profits = [33,20,19,87]
+Output: -1
+Explanation: We can't select any triplet of indices such that the condition holds, so we return -1.
+
+ +

 

+

Constraints:

+ + + +## 解法 + + + + + +### **Python3** + + + +```python + +``` + +### **Java** + + + +```java + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README_EN.md b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README_EN.md new file mode 100644 index 0000000000000..741c0fdec0783 --- /dev/null +++ b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/README_EN.md @@ -0,0 +1,89 @@ +# [2921. Maximum Profitable Triplets With Increasing Prices II](https://leetcode.com/problems/maximum-profitable-triplets-with-increasing-prices-ii) + +[中文文档](/solution/2900-2999/2921.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20II/README.md) + +## Description + +

Given the 0-indexed arrays prices and profits of length n. There are n items in an store where the ith item has a price of prices[i] and a profit of profits[i].

+ +

We have to pick three items with the following condition:

+ + + +

If we pick items with indices i, j and k satisfying the above condition, the profit would be profits[i] + profits[j] + profits[k].

+ +

Return the maximum profit we can get, and -1 if it's not possible to pick three items with the given condition.

+ +

 

+

Example 1:

+ +
+Input: prices = [10,2,3,4], profits = [100,2,7,10]
+Output: 19
+Explanation: We can't pick the item with index i=0 since there are no indices j and k such that the condition holds.
+So the only triplet we can pick, are the items with indices 1, 2 and 3 and it's a valid pick since prices[1] < prices[2] < prices[3].
+The answer would be sum of their profits which is 2 + 7 + 10 = 19.
+ +

Example 2:

+ +
+Input: prices = [1,2,3,4,5], profits = [1,5,3,4,6]
+Output: 15
+Explanation: We can select any triplet of items since for each triplet of indices i, j and k such that i < j < k, the condition holds.
+Therefore the maximum profit we can get would be the 3 most profitable items which are indices 1, 3 and 4.
+The answer would be sum of their profits which is 5 + 4 + 6 = 15.
+ +

Example 3:

+ +
+Input: prices = [4,3,2,1], profits = [33,20,19,87]
+Output: -1
+Explanation: We can't select any triplet of indices such that the condition holds, so we return -1.
+
+ +

 

+

Constraints:

+ + + +## Solutions + + + +### **Python3** + +```python + +``` + +### **Java** + +```java + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **...** + +``` + +``` + + diff --git a/solution/2900-2999/2922.Market Analysis III/README.md b/solution/2900-2999/2922.Market Analysis III/README.md new file mode 100644 index 0000000000000..989e1a11a77a8 --- /dev/null +++ b/solution/2900-2999/2922.Market Analysis III/README.md @@ -0,0 +1,133 @@ +# [2922. Market Analysis III](https://leetcode.cn/problems/market-analysis-iii) + +[English Version](/solution/2900-2999/2922.Market%20Analysis%20III/README_EN.md) + +## 题目描述 + + + +

Table: Users

+ +
++----------------+---------+
+| Column Name    | Type    |
++----------------+---------+
+| seller_id      | int     |
+| join_date      | date    |
+| favorite_brand | varchar |
++----------------+---------+
+seller_id is the primary key for this table.
+This table contains seller id, join date, and favorite brand of sellers.
+
+ +

Table: Items

+ +
++---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| item_id       | int     |
+| item_brand    | varchar |
++---------------+---------+
+item_id is the primary key for this table.
+This table contains item id and item brand.
+ +

Table: Orders

+ +
++---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| order_id      | int     |
+| order_date    | date    |
+| item_id       | int     |
+| seller_id     | int     |
++---------------+---------+
+order_id is the primary key for this table.
+item_id is a foreign key to the Items table.
+seller_id is a foreign key to the Users table.
+This table contains order id, order date, item id and seller id.
+ +

Write a solution to find the top seller who has sold the highest number of unique items with a different brand than their favorite brand. If there are multiple sellers with the same highest count, return all of them.

+ +

Return the result table ordered by seller_id in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
+Input: 
+Users table:
++-----------+------------+----------------+
+| seller_id | join_date  | favorite_brand |
++-----------+------------+----------------+
+| 1         | 2019-01-01 | Lenovo         |
+| 2         | 2019-02-09 | Samsung        |
+| 3         | 2019-01-19 | LG             |
++-----------+------------+----------------+
+Orders table:
++----------+------------+---------+-----------+
+| order_id | order_date | item_id | seller_id |
++----------+------------+---------+-----------+
+| 1        | 2019-08-01 | 4       | 2         |
+| 2        | 2019-08-02 | 2       | 3         |
+| 3        | 2019-08-03 | 3       | 3         |
+| 4        | 2019-08-04 | 1       | 2         |
+| 5        | 2019-08-04 | 4       | 2         |
++----------+------------+---------+-----------+
+Items table:
++---------+------------+
+| item_id | item_brand |
++---------+------------+
+| 1       | Samsung    |
+| 2       | Lenovo     |
+| 3       | LG         |
+| 4       | HP         |
++---------+------------+
+Output: 
++-----------+-----------+
+| seller_id | num_items |
++-----------+-----------+
+| 2         | 1         |
+| 3         | 1         |
++-----------+-----------+
+Explanation: 
+- 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.
+- 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.
+Since seller_ids 2 and 3 have the same count of one item each, they both will be displayed in the output.
+ +## 解法 + + + +**方法一:等值连接 + 分组 + 子查询** + +我们可以使用等值连接,将 `Orders` 表和 `Users` 表按照 `seller_id` 进行连接,接着再按照 `item_id` 连接 `Items`,筛选出 `item_brand` 不等于 `favorite_brand` 的记录,然后按照 `seller_id` 进行分组,统计每个 `seller_id` 对应的 `item_id` 的个数,最后再使用子查询,找出 `item_id` 个数最多的 `seller_id`。 + + + +### **SQL** + + + +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT seller_id, COUNT(DISTINCT item_id) AS num_items + FROM + Orders + JOIN Users USING (seller_id) + JOIN Items USING (item_id) + WHERE item_brand != favorite_brand + GROUP BY 1 + ) +SELECT seller_id, num_items +FROM T +WHERE num_items = (SELECT MAX(num_items) FROM T) +ORDER BY 1; +``` + + diff --git a/solution/2900-2999/2922.Market Analysis III/README_EN.md b/solution/2900-2999/2922.Market Analysis III/README_EN.md new file mode 100644 index 0000000000000..84ece47fe6a3d --- /dev/null +++ b/solution/2900-2999/2922.Market Analysis III/README_EN.md @@ -0,0 +1,127 @@ +# [2922. Market Analysis III](https://leetcode.com/problems/market-analysis-iii) + +[中文文档](/solution/2900-2999/2922.Market%20Analysis%20III/README.md) + +## Description + +

Table: Users

+ +
++----------------+---------+
+| Column Name    | Type    |
++----------------+---------+
+| seller_id      | int     |
+| join_date      | date    |
+| favorite_brand | varchar |
++----------------+---------+
+seller_id is the primary key for this table.
+This table contains seller id, join date, and favorite brand of sellers.
+
+ +

Table: Items

+ +
++---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| item_id       | int     |
+| item_brand    | varchar |
++---------------+---------+
+item_id is the primary key for this table.
+This table contains item id and item brand.
+ +

Table: Orders

+ +
++---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| order_id      | int     |
+| order_date    | date    |
+| item_id       | int     |
+| seller_id     | int     |
++---------------+---------+
+order_id is the primary key for this table.
+item_id is a foreign key to the Items table.
+seller_id is a foreign key to the Users table.
+This table contains order id, order date, item id and seller id.
+ +

Write a solution to find the top seller who has sold the highest number of unique items with a different brand than their favorite brand. If there are multiple sellers with the same highest count, return all of them.

+ +

Return the result table ordered by seller_id in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example 1:

+ +
+Input: 
+Users table:
++-----------+------------+----------------+
+| seller_id | join_date  | favorite_brand |
++-----------+------------+----------------+
+| 1         | 2019-01-01 | Lenovo         |
+| 2         | 2019-02-09 | Samsung        |
+| 3         | 2019-01-19 | LG             |
++-----------+------------+----------------+
+Orders table:
++----------+------------+---------+-----------+
+| order_id | order_date | item_id | seller_id |
++----------+------------+---------+-----------+
+| 1        | 2019-08-01 | 4       | 2         |
+| 2        | 2019-08-02 | 2       | 3         |
+| 3        | 2019-08-03 | 3       | 3         |
+| 4        | 2019-08-04 | 1       | 2         |
+| 5        | 2019-08-04 | 4       | 2         |
++----------+------------+---------+-----------+
+Items table:
++---------+------------+
+| item_id | item_brand |
++---------+------------+
+| 1       | Samsung    |
+| 2       | Lenovo     |
+| 3       | LG         |
+| 4       | HP         |
++---------+------------+
+Output: 
++-----------+-----------+
+| seller_id | num_items |
++-----------+-----------+
+| 2         | 1         |
+| 3         | 1         |
++-----------+-----------+
+Explanation: 
+- 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.
+- 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.
+Since seller_ids 2 and 3 have the same count of one item each, they both will be displayed in the output.
+ +## Solutions + +**Solution 1: Equijoin + Grouping + Subquery** + +We can use equijoin to connect the `Orders` table and the `Users` table according to `seller_id`, then connect `Items` according to `item_id`, and filter out the records where `item_brand` is not equal to `favorite_brand`. Then, group by `seller_id` and count the number of `item_id` corresponding to each `seller_id`. Finally, use a subquery to find the `seller_id` with the most `item_id`. + + + +### **SQL** + +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT seller_id, COUNT(DISTINCT item_id) AS num_items + FROM + Orders + JOIN Users USING (seller_id) + JOIN Items USING (item_id) + WHERE item_brand != favorite_brand + GROUP BY 1 + ) +SELECT seller_id, num_items +FROM T +WHERE num_items = (SELECT MAX(num_items) FROM T) +ORDER BY 1; +``` + + diff --git a/solution/2900-2999/2922.Market Analysis III/Solution.sql b/solution/2900-2999/2922.Market Analysis III/Solution.sql new file mode 100644 index 0000000000000..1f7ace429807c --- /dev/null +++ b/solution/2900-2999/2922.Market Analysis III/Solution.sql @@ -0,0 +1,15 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT seller_id, COUNT(DISTINCT item_id) AS num_items + FROM + Orders + JOIN Users USING (seller_id) + JOIN Items USING (item_id) + WHERE item_brand != favorite_brand + GROUP BY 1 + ) +SELECT seller_id, num_items +FROM T +WHERE num_items = (SELECT MAX(num_items) FROM T) +ORDER BY 1; diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 87a73ec15b617..27fbcc9a6a064 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -246,6 +246,7 @@ | 2853 | [最高薪水差异](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md) | `数据库` | 简单 | 🔒 | | 2854 | [滚动平均步数](/solution/2800-2899/2854.Rolling%20Average%20Steps/README.md) | `数据库` | 中等 | 🔒 | | 2893 | [计算每个区间内的订单](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md) | `数据库` | 中等 | 🔒 | +| 2922 | [Market Analysis III](/solution/2900-2999/2922.Market%20Analysis%20III/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 9ea0768a5145f..a0c9c24b672ca 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -244,6 +244,7 @@ Press Control + F(or Command + F on | 2853 | [Highest Salaries Difference](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README_EN.md) | `Database` | Easy | 🔒 | | 2854 | [Rolling Average Steps](/solution/2800-2899/2854.Rolling%20Average%20Steps/README_EN.md) | `Database` | Medium | 🔒 | | 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md) | `Database` | Medium | 🔒 | +| 2922 | [Market Analysis III](/solution/2900-2999/2922.Market%20Analysis%20III/README_EN.md) | | Medium | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index e79a603e62519..a16e854124e59 100644 --- a/solution/README.md +++ b/solution/README.md @@ -2931,6 +2931,8 @@ | 2918 | [数组的最小相等和](/solution/2900-2999/2918.Minimum%20Equal%20Sum%20of%20Two%20Arrays%20After%20Replacing%20Zeros/README.md) | | 中等 | 第 369 场周赛 | | 2919 | [使数组变美的最小增量运算数](/solution/2900-2999/2919.Minimum%20Increment%20Operations%20to%20Make%20Array%20Beautiful/README.md) | | 中等 | 第 369 场周赛 | | 2920 | [收集所有金币可获得的最大积分](/solution/2900-2999/2920.Maximum%20Points%20After%20Collecting%20Coins%20From%20All%20Nodes/README.md) | | 困难 | 第 369 场周赛 | +| 2921 | [Maximum Profitable Triplets With Increasing Prices II](/solution/2900-2999/2921.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20II/README.md) | | 困难 | 🔒 | +| 2922 | [Market Analysis III](/solution/2900-2999/2922.Market%20Analysis%20III/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 3f01369883802..7dacca89c3113 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -2929,6 +2929,8 @@ Press Control + F(or Command + F on | 2918 | [Minimum Equal Sum of Two Arrays After Replacing Zeros](/solution/2900-2999/2918.Minimum%20Equal%20Sum%20of%20Two%20Arrays%20After%20Replacing%20Zeros/README_EN.md) | | Medium | Weekly Contest 369 | | 2919 | [Minimum Increment Operations to Make Array Beautiful](/solution/2900-2999/2919.Minimum%20Increment%20Operations%20to%20Make%20Array%20Beautiful/README_EN.md) | | Medium | Weekly Contest 369 | | 2920 | [Maximum Points After Collecting Coins From All Nodes](/solution/2900-2999/2920.Maximum%20Points%20After%20Collecting%20Coins%20From%20All%20Nodes/README_EN.md) | | Hard | Weekly Contest 369 | +| 2921 | [Maximum Profitable Triplets With Increasing Prices II](/solution/2900-2999/2921.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20II/README_EN.md) | | Hard | 🔒 | +| 2922 | [Market Analysis III](/solution/2900-2999/2922.Market%20Analysis%20III/README_EN.md) | | Medium | 🔒 | ## Copyright diff --git a/solution/database-summary.md b/solution/database-summary.md index b3f657b752972..816b294e2991e 100644 --- a/solution/database-summary.md +++ b/solution/database-summary.md @@ -236,3 +236,4 @@ - [2853.最高薪水差异](/database-solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md) - [2854.滚动平均步数](/database-solution/2800-2899/2854.Rolling%20Average%20Steps/README.md) - [2893.计算每个区间内的订单](/database-solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md) + - [2922.Market Analysis III](/database-solution/2900-2999/2922.Market%20Analysis%20III/README.md) diff --git a/solution/database-summary_en.md b/solution/database-summary_en.md index d9ddddc549a2a..9908fde08b21d 100644 --- a/solution/database-summary_en.md +++ b/solution/database-summary_en.md @@ -236,3 +236,4 @@ - [2853.Highest Salaries Difference](/database-solution/2800-2899/2853.Highest%20Salaries%20Difference/README_EN.md) - [2854.Rolling Average Steps](/database-solution/2800-2899/2854.Rolling%20Average%20Steps/README_EN.md) - [2893.Calculate Orders Within Each Interval](/database-solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md) + - [2922.Market Analysis III](/database-solution/2900-2999/2922.Market%20Analysis%20III/README_EN.md) diff --git a/solution/summary.md b/solution/summary.md index 104a5e9ad401c..ef1ac436a2e83 100644 --- a/solution/summary.md +++ b/solution/summary.md @@ -2978,3 +2978,5 @@ - [2918.数组的最小相等和](/solution/2900-2999/2918.Minimum%20Equal%20Sum%20of%20Two%20Arrays%20After%20Replacing%20Zeros/README.md) - [2919.使数组变美的最小增量运算数](/solution/2900-2999/2919.Minimum%20Increment%20Operations%20to%20Make%20Array%20Beautiful/README.md) - [2920.收集所有金币可获得的最大积分](/solution/2900-2999/2920.Maximum%20Points%20After%20Collecting%20Coins%20From%20All%20Nodes/README.md) + - [2921.Maximum Profitable Triplets With Increasing Prices II](/solution/2900-2999/2921.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20II/README.md) + - [2922.Market Analysis III](/solution/2900-2999/2922.Market%20Analysis%20III/README.md) diff --git a/solution/summary_en.md b/solution/summary_en.md index ebdf1445df18f..7c257e7be933e 100644 --- a/solution/summary_en.md +++ b/solution/summary_en.md @@ -2978,3 +2978,5 @@ - [2918.Minimum Equal Sum of Two Arrays After Replacing Zeros](/solution/2900-2999/2918.Minimum%20Equal%20Sum%20of%20Two%20Arrays%20After%20Replacing%20Zeros/README_EN.md) - [2919.Minimum Increment Operations to Make Array Beautiful](/solution/2900-2999/2919.Minimum%20Increment%20Operations%20to%20Make%20Array%20Beautiful/README_EN.md) - [2920.Maximum Points After Collecting Coins From All Nodes](/solution/2900-2999/2920.Maximum%20Points%20After%20Collecting%20Coins%20From%20All%20Nodes/README_EN.md) + - [2921.Maximum Profitable Triplets With Increasing Prices II](/solution/2900-2999/2921.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20II/README_EN.md) + - [2922.Market Analysis III](/solution/2900-2999/2922.Market%20Analysis%20III/README_EN.md)