From 52bd90956e5fec54d53b8f4dd089da0f1b480e43 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 12 Oct 2023 09:10:18 +0800 Subject: [PATCH] feat: update solutions to lc problems * No.0182.Duplicate Emails * No.1050.Actors and Directors Who Cooperated At Least Three Times * No.1495.Friendly Movies Streamed Last Month * No.1511.Customer Order Frequency * No.1587.Bank Account Summary II * No.1693.Daily Leads and Partners * No.1699.Number of Calls Between Two Persons * No.2562.Find the Array Concatenation Value --- .prettierignore | 1 - .prettierrc | 2 ++ .../0100-0199/0182.Duplicate Emails/README.md | 15 ++++++++--- .../0182.Duplicate Emails/README_EN.md | 15 ++++++++--- .../0182.Duplicate Emails/Solution.sql | 7 +++--- .../README.md | 4 +-- .../README_EN.md | 4 ++- .../README.md | 25 ++++++++----------- .../README_EN.md | 25 ++++++++----------- .../Solution.sql | 19 +++++--------- .../1511.Customer Order Frequency/README.md | 23 +++++++++-------- .../README_EN.md | 23 +++++++++-------- .../Solution.sql | 19 +++++++------- .../1587.Bank Account Summary II/README.md | 23 ++++++----------- .../1587.Bank Account Summary II/README_EN.md | 23 ++++++----------- .../1587.Bank Account Summary II/Solution.sql | 13 +++++----- .../1693.Daily Leads and Partners/README.md | 11 +++++--- .../README_EN.md | 11 +++++--- .../Solution.sql | 7 +++--- .../README.md | 25 +++++++++++++++---- .../README_EN.md | 25 +++++++++++++++---- .../Solution.sql | 10 ++++---- .../README.md | 2 +- .../README_EN.md | 6 +++++ 24 files changed, 191 insertions(+), 147 deletions(-) diff --git a/.prettierignore b/.prettierignore index 92592c9d189ab..501eb3e38ee8d 100644 --- a/.prettierignore +++ b/.prettierignore @@ -16,7 +16,6 @@ node_modules/ /solution/0100-0199/0177.Nth Highest Salary/Solution.sql /solution/1400-1499/1454.Active Users/Solution.sql /solution/1400-1499/1484.Group Sold Products By The Date/Solution.sql -/solution/1500-1599/1511.Customer Order Frequency/Solution.sql /solution/1600-1699/1613.Find the Missing IDs/Solution.sql /solution/1600-1699/1635.Hopper Company Queries I/Solution.sql /solution/1600-1699/1651.Hopper Company Queries III/Solution.sql diff --git a/.prettierrc b/.prettierrc index 6c5eecb331c09..c3622c88e1f05 100644 --- a/.prettierrc +++ b/.prettierrc @@ -34,8 +34,10 @@ "solution/1400-1499/1445.Apples & Oranges/Solution.sql", "solution/1400-1499/1479.Sales by Day of the Week/Solution.sql", "solution/1500-1599/1501.Countries You Can Safely Invest In/Solution.sql", + "solution/1500-1599/1511.Customer Order Frequency/Solution.sql", "solution/1500-1599/1555.Bank Account Summary/Solution.sql", "solution/1600-1699/1667.Fix Names in a Table/Solution.sql", + "solution/1600-1699/1699.Number of Calls Between Two Persons/Solution.sql", "solution/1700-1799/1777.Product's Price for Each Store/Solution.sql", "solution/1900-1999/1934.Confirmation Rate/Solution.sql", "solution/1900-1999/1972.First and Last Call On the Same Day/Solution.sql", diff --git a/solution/0100-0199/0182.Duplicate Emails/README.md b/solution/0100-0199/0182.Duplicate Emails/README.md index 5a95c4292d88f..2b1746daf5bb3 100644 --- a/solution/0100-0199/0182.Duplicate Emails/README.md +++ b/solution/0100-0199/0182.Duplicate Emails/README.md @@ -55,15 +55,24 @@ Person 表: +**方法一:分组统计** + +我们可以使用 `GROUP BY` 语句,按照 `email` 字段进行分组,然后使用 `HAVING` 语句,筛选出现次数大于 $1$ 的 `email`。 + +**方法二:自连接** + +我们可以使用自连接的方法,将 `Person` 表自身连接一次,然后筛选出 `id` 不同,但 `email` 相同的记录。 + ### **SQL** ```sql -SELECT Email +# Write your MySQL query statement below +SELECT email FROM Person -GROUP BY Email -HAVING count(Email) > 1; +GROUP BY 1 +HAVING count(1) > 1; ``` ```sql diff --git a/solution/0100-0199/0182.Duplicate Emails/README_EN.md b/solution/0100-0199/0182.Duplicate Emails/README_EN.md index e0508a5a7bf18..13622f64853f2 100644 --- a/solution/0100-0199/0182.Duplicate Emails/README_EN.md +++ b/solution/0100-0199/0182.Duplicate Emails/README_EN.md @@ -49,15 +49,24 @@ Person table: ## Solutions +**Solution 1: Group By + Having** + +We can use the `GROUP BY` statement to group the data by the `email` field, and then use the `HAVING` statement to filter out the `email` addresses that appear more than once. + +**Solution 2: Self-Join** + +We can use a self-join to join the `Person` table with itself, and then filter out the records where the `id` is different but the `email` is the same. + ### **SQL** ```sql -SELECT Email +# Write your MySQL query statement below +SELECT email FROM Person -GROUP BY Email -HAVING count(Email) > 1; +GROUP BY 1 +HAVING count(1) > 1; ``` ```sql diff --git a/solution/0100-0199/0182.Duplicate Emails/Solution.sql b/solution/0100-0199/0182.Duplicate Emails/Solution.sql index debb6893a8fae..849ea0dce7562 100644 --- a/solution/0100-0199/0182.Duplicate Emails/Solution.sql +++ b/solution/0100-0199/0182.Duplicate Emails/Solution.sql @@ -1,4 +1,5 @@ -SELECT Email +# Write your MySQL query statement below +SELECT email FROM Person -GROUP BY Email -HAVING count(Email) > 1; +GROUP BY 1 +HAVING count(1) > 1; diff --git a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md index ba3b9efb08115..8c85d23ebead2 100644 --- a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md +++ b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md @@ -54,9 +54,9 @@ ActorDirector 表: -**方法一:使用 `GROUP BY` + `HAVING`** +**方法一:分组统计** -我们将 `ActorDirector` 表按照 `actor_id` 和 `director_id` 进行分组,然后使用 `HAVING` 过滤出合作次数大于等于 $3$ 次的组。 +我们可以使用 `GROUP BY` 语句,按照 `actor_id` 和 `director_id` 字段进行分组,然后使用 `HAVING` 语句,筛选出现次数大于等于 $3$ 的 `actor_id` 和 `director_id`。 diff --git a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md index bb89e8158975f..87f42913c00bb 100644 --- a/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md +++ b/solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md @@ -53,7 +53,9 @@ ActorDirector table: ## Solutions -Use `GROUP BY` & `HAVING`. +**Solution 1: Group By + Having** + +We can use the `GROUP BY` statement to group the data by the `actor_id` and `director_id` fields, and then use the `HAVING` statement to filter out the `actor_id` and `director_id` that appear at least three times. diff --git a/solution/1400-1499/1495.Friendly Movies Streamed Last Month/README.md b/solution/1400-1499/1495.Friendly Movies Streamed Last Month/README.md index 0ddbf408adccd..60a54f7d04787 100644 --- a/solution/1400-1499/1495.Friendly Movies Streamed Last Month/README.md +++ b/solution/1400-1499/1495.Friendly Movies Streamed Last Month/README.md @@ -91,29 +91,24 @@ TVProgram 表: +**方法一:等值连接 + 条件筛选** + +我们可以先通过等值连接将两张表按照 `content_id` 字段连接起来,然后再通过条件筛选出在 $2020$ 年 $6$ 月份播放的儿童适宜电影。 + ### **SQL** ```sql -SELECT DISTINCT - title +# Write your MySQL query statement below +SELECT DISTINCT title FROM - Content - INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id + TVProgram + JOIN Content USING (content_id) WHERE - content_type = 'Movies' + date_format(program_date, '%Y%m') = '202006' AND kids_content = 'Y' - AND program_date BETWEEN '2020-06-01' AND '2020-06-30'; -``` - -```sql -SELECT DISTINCT - title -FROM - Content - INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id -WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020); + AND content_type = 'Movies'; ``` diff --git a/solution/1400-1499/1495.Friendly Movies Streamed Last Month/README_EN.md b/solution/1400-1499/1495.Friendly Movies Streamed Last Month/README_EN.md index 9ea81f8c703a5..b3e14e81307ba 100644 --- a/solution/1400-1499/1495.Friendly Movies Streamed Last Month/README_EN.md +++ b/solution/1400-1499/1495.Friendly Movies Streamed Last Month/README_EN.md @@ -87,27 +87,22 @@ Content table: ## Solutions +**Solution 1: Equi-Join + Conditional Filtering** + +We can first use an equi-join to join the two tables based on the `content_id` field, and then use conditional filtering to select the child-friendly movies that were played in June 2020. + ```sql -SELECT DISTINCT - title +# Write your MySQL query statement below +SELECT DISTINCT title FROM - Content - INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id + TVProgram + JOIN Content USING (content_id) WHERE - content_type = 'Movies' + date_format(program_date, '%Y%m') = '202006' AND kids_content = 'Y' - AND program_date BETWEEN '2020-06-01' AND '2020-06-30'; -``` - -```sql -SELECT DISTINCT - title -FROM - Content - INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id -WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020); + AND content_type = 'Movies'; ``` diff --git a/solution/1400-1499/1495.Friendly Movies Streamed Last Month/Solution.sql b/solution/1400-1499/1495.Friendly Movies Streamed Last Month/Solution.sql index 5e2af4088ac7a..85b242354030b 100644 --- a/solution/1400-1499/1495.Friendly Movies Streamed Last Month/Solution.sql +++ b/solution/1400-1499/1495.Friendly Movies Streamed Last Month/Solution.sql @@ -1,16 +1,9 @@ -SELECT DISTINCT - title +# Write your MySQL query statement below +SELECT DISTINCT title FROM - Content - INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id + TVProgram + JOIN Content USING (content_id) WHERE - content_type = 'Movies' + date_format(program_date, '%Y%m') = '202006' AND kids_content = 'Y' - AND program_date BETWEEN '2020-06-01' AND '2020-06-30'; - -SELECT DISTINCT - title -FROM - Content - INNER JOIN TVProgram ON Content.content_id = TVProgram.content_id -WHERE kids_content = 'Y' AND (MONTH(program_date), YEAR(program_date)) = (6, 2020); + AND content_type = 'Movies'; diff --git a/solution/1500-1599/1511.Customer Order Frequency/README.md b/solution/1500-1599/1511.Customer Order Frequency/README.md index ce1319eb48dca..d48a056cc26e7 100644 --- a/solution/1500-1599/1511.Customer Order Frequency/README.md +++ b/solution/1500-1599/1511.Customer Order Frequency/README.md @@ -117,23 +117,26 @@ Moustafa 在2020年6月花费了110 (10 * 2 + 45 * 2), 在7月花费了0. +**方法一:等值连接 + 分组求和** + +我们可以使用 `JOIN` 语句,连接 `Orders` 和 `Product` 表,再连接 `Customers` 表,筛选出 `order_date` 在 $2020$ 年的记录,然后使用 `GROUP BY` 语句,按照 `customer_id` 分组,使用 `HAVING` 语句,筛选出 $6$ 月和 $7$ 月花费大于等于 $100$ 的客户。 + ### **SQL** ```sql # Write your MySQL query statement below -SELECT - c.customer_id AS CUSTOMER_ID, - c.name AS NAME +SELECT customer_id, name FROM - Customers c, Product p, Orders o -WHERE - c.customer_id = o.customer_id -AND p.product_id = o.product_id -GROUP BY c.customer_id -HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100 -AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100; + Orders + JOIN Product USING (product_id) + JOIN Customers USING (customer_id) +WHERE year(order_date) = 2020 +GROUP BY 1 +HAVING + sum(if(month(order_date) = 6, quantity * price, 0)) >= 100 + AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100; ``` diff --git a/solution/1500-1599/1511.Customer Order Frequency/README_EN.md b/solution/1500-1599/1511.Customer Order Frequency/README_EN.md index 4c0fc08adeb0a..91e87ee6d3d08 100644 --- a/solution/1500-1599/1511.Customer Order Frequency/README_EN.md +++ b/solution/1500-1599/1511.Customer Order Frequency/README_EN.md @@ -112,23 +112,26 @@ Moustafa spent 110 (10 * 2 + 45 * 2) in June and 0 in July 2020. ## Solutions +**Solution 1: Join + Group By + Having** + +We can use the `JOIN` statement to join the `Orders` table and the `Product` table, and then join the result with the `Customers` table. We can filter out the records where the `order_date` is not in the year $2020$, and then use the `GROUP BY` statement to group the data by `customer_id`. Finally, we can use the `HAVING` statement to filter out the customers whose spending in June and July is greater than or equal to $100$. + ### **SQL** ```sql # Write your MySQL query statement below -SELECT - c.customer_id AS CUSTOMER_ID, - c.name AS NAME +SELECT customer_id, name FROM - Customers c, Product p, Orders o -WHERE - c.customer_id = o.customer_id -AND p.product_id = o.product_id -GROUP BY c.customer_id -HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100 -AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100; + Orders + JOIN Product USING (product_id) + JOIN Customers USING (customer_id) +WHERE year(order_date) = 2020 +GROUP BY 1 +HAVING + sum(if(month(order_date) = 6, quantity * price, 0)) >= 100 + AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100; ``` diff --git a/solution/1500-1599/1511.Customer Order Frequency/Solution.sql b/solution/1500-1599/1511.Customer Order Frequency/Solution.sql index 519cd795bfaa2..da8aaa9d75b7e 100644 --- a/solution/1500-1599/1511.Customer Order Frequency/Solution.sql +++ b/solution/1500-1599/1511.Customer Order Frequency/Solution.sql @@ -1,12 +1,11 @@ # Write your MySQL query statement below -SELECT - c.customer_id AS CUSTOMER_ID, - c.name AS NAME +SELECT customer_id, name FROM - Customers c, Product p, Orders o -WHERE - c.customer_id = o.customer_id -AND p.product_id = o.product_id -GROUP BY c.customer_id -HAVING sum(if(month(o.order_date)=6, price*quantity, 0)) >= 100 -AND sum(if(month(o.order_date)=7, price*quantity, 0)) >= 100; \ No newline at end of file + Orders + JOIN Product USING (product_id) + JOIN Customers USING (customer_id) +WHERE year(order_date) = 2020 +GROUP BY 1 +HAVING + sum(if(month(order_date) = 6, quantity * price, 0)) >= 100 + AND sum(if(month(order_date) = 7, quantity * price, 0)) >= 100; diff --git a/solution/1500-1599/1587.Bank Account Summary II/README.md b/solution/1500-1599/1587.Bank Account Summary II/README.md index b582520dc6d95..40a3b0975c7b9 100644 --- a/solution/1500-1599/1587.Bank Account Summary II/README.md +++ b/solution/1500-1599/1587.Bank Account Summary II/README.md @@ -90,33 +90,26 @@ Charlie 的余额为(6000 + 6000 - 4000) = 8000. +**方法一:等值连接 + 分组求和** + +我们可以使用等值连接,将 `Users` 和 `Transactions` 表按照 `account` 列连接起来,然后按照 `account` 列分组求和,最后筛选出余额大于 $10000$ 的用户。 + ### **SQL** -```sql -SELECT - u.name, - SUM(t.amount) AS balance -FROM - users AS u - JOIN transactions AS t ON u.account = t.account -GROUP BY name -HAVING SUM(t.amount) > 10000; -``` - ```sql # Write your MySQL query statement below SELECT name, sum(amount) AS balance FROM - Users AS u - LEFT JOIN Transactions AS t ON u.account = t.account -GROUP BY u.account -HAVING sum(amount) > 10000; + Users + JOIN Transactions USING (account) +GROUP BY account +HAVING balance > 10000; ``` diff --git a/solution/1500-1599/1587.Bank Account Summary II/README_EN.md b/solution/1500-1599/1587.Bank Account Summary II/README_EN.md index 8e2861b10079d..9522b819d1c93 100644 --- a/solution/1500-1599/1587.Bank Account Summary II/README_EN.md +++ b/solution/1500-1599/1587.Bank Account Summary II/README_EN.md @@ -84,31 +84,24 @@ Charlie's balance is (6000 + 6000 - 4000) = 8000. ## Solutions +**Solution 1: Equi-Join + Group By + Sum** + +We can use an equi-join to join the `Users` table and the `Transactions` table on the condition of `account`, and then group by `account` to calculate the balance for each account using the `SUM` function. Finally, we can filter out the users whose balance is less than or equal to $10000$. + ### **SQL** -```sql -SELECT - u.name, - SUM(t.amount) AS balance -FROM - users AS u - JOIN transactions AS t ON u.account = t.account -GROUP BY name -HAVING SUM(t.amount) > 10000; -``` - ```sql # Write your MySQL query statement below SELECT name, sum(amount) AS balance FROM - Users AS u - LEFT JOIN Transactions AS t ON u.account = t.account -GROUP BY u.account -HAVING sum(amount) > 10000; + Users + JOIN Transactions USING (account) +GROUP BY account +HAVING balance > 10000; ``` diff --git a/solution/1500-1599/1587.Bank Account Summary II/Solution.sql b/solution/1500-1599/1587.Bank Account Summary II/Solution.sql index 3a3b3d52eb900..0e26be4b79380 100644 --- a/solution/1500-1599/1587.Bank Account Summary II/Solution.sql +++ b/solution/1500-1599/1587.Bank Account Summary II/Solution.sql @@ -1,8 +1,9 @@ +# Write your MySQL query statement below SELECT - u.name, - SUM(t.amount) AS balance + name, + sum(amount) AS balance FROM - users AS u - JOIN transactions AS t ON u.account = t.account -GROUP BY name -HAVING SUM(t.amount) > 10000; + Users + JOIN Transactions USING (account) +GROUP BY account +HAVING balance > 10000; diff --git a/solution/1600-1699/1693.Daily Leads and Partners/README.md b/solution/1600-1699/1693.Daily Leads and Partners/README.md index 1a3564bfa5d06..1f59eb7bc7d3f 100644 --- a/solution/1600-1699/1693.Daily Leads and Partners/README.md +++ b/solution/1600-1699/1693.Daily Leads and Partners/README.md @@ -67,18 +67,23 @@ DailySales 表: +**方法一:分组统计** + +我们可以使用 `GROUP BY` 语句,按照 `date_id` 和 `make_name` 字段进行分组,然后使用 `COUNT(DISTINCT)` 函数,统计 `lead_id` 和 `partner_id` 的不同值的数量。 + ### **SQL** ```sql +# Write your MySQL query statement below SELECT date_id, make_name, - COUNT(DISTINCT lead_id) AS unique_leads, - COUNT(DISTINCT partner_id) AS unique_partners + count(DISTINCT lead_id) AS unique_leads, + count(DISTINCT partner_id) AS unique_partners FROM DailySales -GROUP BY date_id, make_name; +GROUP BY 1, 2; ``` diff --git a/solution/1600-1699/1693.Daily Leads and Partners/README_EN.md b/solution/1600-1699/1693.Daily Leads and Partners/README_EN.md index 52a2f5a2065c7..9e6831c5b464e 100644 --- a/solution/1600-1699/1693.Daily Leads and Partners/README_EN.md +++ b/solution/1600-1699/1693.Daily Leads and Partners/README_EN.md @@ -64,18 +64,23 @@ For 2020-12-7, toyota gets leads = [0] and partners = [1, 2] while honda gets le ## Solutions +**Solution 1: Group By + Count Distinct** + +We can use the `GROUP BY` statement to group the data by the `date_id` and `make_name` fields, and then use the `COUNT(DISTINCT)` function to count the number of distinct values for `lead_id` and `partner_id`. + ### **SQL** ```sql +# Write your MySQL query statement below SELECT date_id, make_name, - COUNT(DISTINCT lead_id) AS unique_leads, - COUNT(DISTINCT partner_id) AS unique_partners + count(DISTINCT lead_id) AS unique_leads, + count(DISTINCT partner_id) AS unique_partners FROM DailySales -GROUP BY date_id, make_name; +GROUP BY 1, 2; ``` diff --git a/solution/1600-1699/1693.Daily Leads and Partners/Solution.sql b/solution/1600-1699/1693.Daily Leads and Partners/Solution.sql index 56e97275849bc..33bb3386293ed 100644 --- a/solution/1600-1699/1693.Daily Leads and Partners/Solution.sql +++ b/solution/1600-1699/1693.Daily Leads and Partners/Solution.sql @@ -1,7 +1,8 @@ +# Write your MySQL query statement below SELECT date_id, make_name, - COUNT(DISTINCT lead_id) AS unique_leads, - COUNT(DISTINCT partner_id) AS unique_partners + count(DISTINCT lead_id) AS unique_leads, + count(DISTINCT partner_id) AS unique_partners FROM DailySales -GROUP BY date_id, make_name; +GROUP BY 1, 2; diff --git a/solution/1600-1699/1699.Number of Calls Between Two Persons/README.md b/solution/1600-1699/1699.Number of Calls Between Two Persons/README.md index 09a121c363e35..54cd130adff7b 100644 --- a/solution/1600-1699/1699.Number of Calls Between Two Persons/README.md +++ b/solution/1600-1699/1699.Number of Calls Between Two Persons/README.md @@ -64,6 +64,10 @@ Calls 表: +**方法一:分组求和统计** + +我们可以用 `if` 函数或者 `least` 和 `greatest` 函数来将 `from_id` 和 `to_id` 转换成 `person1` 和 `person2`,然后按照 `person1` 和 `person2` 分组求和统计即可。 + ### **SQL** @@ -71,12 +75,23 @@ Calls 表: ```sql # Write your MySQL query statement below SELECT - from_id AS person1, - to_id AS person2, - COUNT(1) AS call_count, - SUM(duration) AS total_duration + if(from_id < to_id, from_id, to_id) AS person1, + if(from_id < to_id, to_id, from_id) AS person2, + count(1) AS call_count, + sum(duration) AS total_duration +FROM Calls +GROUP BY 1, 2; +``` + +```sql +# Write your MySQL query statement below +SELECT + least(from_id, to_id) AS person1, + greatest(from_id, to_id) AS person2, + count(1) AS call_count, + sum(duration) AS total_duration FROM Calls -GROUP BY LEAST(from_id, to_id), GREATEST(from_id, to_id); +GROUP BY 1, 2; ``` diff --git a/solution/1600-1699/1699.Number of Calls Between Two Persons/README_EN.md b/solution/1600-1699/1699.Number of Calls Between Two Persons/README_EN.md index bd701f2d77425..c0283f3806a0d 100644 --- a/solution/1600-1699/1699.Number of Calls Between Two Persons/README_EN.md +++ b/solution/1600-1699/1699.Number of Calls Between Two Persons/README_EN.md @@ -60,6 +60,10 @@ Users 3 and 4 had 4 calls and the total duration is 999 (100 + 200 + 200 + 499). ## Solutions +**Solution 1: Grouping and Summing** + +We can use the `if` function or the `least` and `greatest` functions to convert `from_id` and `to_id` into `person1` and `person2`, and then group by `person1` and `person2` and sum the values. + ### **SQL** @@ -67,12 +71,23 @@ Users 3 and 4 had 4 calls and the total duration is 999 (100 + 200 + 200 + 499). ```sql # Write your MySQL query statement below SELECT - from_id AS person1, - to_id AS person2, - COUNT(1) AS call_count, - SUM(duration) AS total_duration + if(from_id < to_id, from_id, to_id) AS person1, + if(from_id < to_id, to_id, from_id) AS person2, + count(1) AS call_count, + sum(duration) AS total_duration +FROM Calls +GROUP BY 1, 2; +``` + +```sql +# Write your MySQL query statement below +SELECT + least(from_id, to_id) AS person1, + greatest(from_id, to_id) AS person2, + count(1) AS call_count, + sum(duration) AS total_duration FROM Calls -GROUP BY LEAST(from_id, to_id), GREATEST(from_id, to_id); +GROUP BY 1, 2; ``` diff --git a/solution/1600-1699/1699.Number of Calls Between Two Persons/Solution.sql b/solution/1600-1699/1699.Number of Calls Between Two Persons/Solution.sql index d7361c7e86572..955c80a3acaa7 100644 --- a/solution/1600-1699/1699.Number of Calls Between Two Persons/Solution.sql +++ b/solution/1600-1699/1699.Number of Calls Between Two Persons/Solution.sql @@ -1,8 +1,8 @@ # Write your MySQL query statement below SELECT - from_id AS person1, - to_id AS person2, - COUNT(1) AS call_count, - SUM(duration) AS total_duration + least(from_id, to_id) AS person1, + greatest(from_id, to_id) AS person2, + count(1) AS call_count, + sum(duration) AS total_duration FROM Calls -GROUP BY LEAST(from_id, to_id), GREATEST(from_id, to_id); +GROUP BY 1, 2; diff --git a/solution/2500-2599/2562.Find the Array Concatenation Value/README.md b/solution/2500-2599/2562.Find the Array Concatenation Value/README.md index bb250393f4705..e0962c8500f48 100644 --- a/solution/2500-2599/2562.Find the Array Concatenation Value/README.md +++ b/solution/2500-2599/2562.Find the Array Concatenation Value/README.md @@ -79,7 +79,7 @@ nums 只有一个元素,所以我们选中 13 并将其加到串联值上, 从数组两端开始,每次取出一个元素,将其与另一个元素拼接,然后将拼接后的结果加到答案中。重复这个过程,直到数组为空。 -时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n \times \log M)$。其中 $n$ 和 $M$ 分别是数组的长度和数组中的最大值。 +时间复杂度 $O(n \times \log M)$,空间复杂度 $O(\log M)$。其中 $n$ 和 $M$ 分别是数组的长度和数组中的最大值。 diff --git a/solution/2500-2599/2562.Find the Array Concatenation Value/README_EN.md b/solution/2500-2599/2562.Find the Array Concatenation Value/README_EN.md index 9c59b80ab1645..ca85244f679e4 100644 --- a/solution/2500-2599/2562.Find the Array Concatenation Value/README_EN.md +++ b/solution/2500-2599/2562.Find the Array Concatenation Value/README_EN.md @@ -78,6 +78,12 @@ Since the concatenation value is 673 so the answer is 673. ## Solutions +**Solution 1: Simulation** + +Starting from both ends of the array, we take out one element at a time, concatenate it with another element, and then add the concatenated result to the answer. We repeat this process until the array is empty. + +The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Here, $n$ and $M$ are the length of the array and the maximum value in the array, respectively. + ### **Python3**