From 8b372ae8cf19f9c75ca5518b50be3c8403461ad5 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 26 Jun 2024 08:10:28 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3198 No.3198.Find Cities in Each State --- .prettierignore | 3 +- .../3198.Find Cities in Each State/README.md | 128 ++++++++++++++++++ .../README_EN.md | 127 +++++++++++++++++ .../Solution.py | 11 ++ .../Solution.sql | 7 + solution/DATABASE_README.md | 1 + solution/DATABASE_README_EN.md | 1 + solution/README.md | 1 + solution/README_EN.md | 1 + 9 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 solution/3100-3199/3198.Find Cities in Each State/README.md create mode 100644 solution/3100-3199/3198.Find Cities in Each State/README_EN.md create mode 100644 solution/3100-3199/3198.Find Cities in Each State/Solution.py create mode 100644 solution/3100-3199/3198.Find Cities in Each State/Solution.sql diff --git a/.prettierignore b/.prettierignore index f76480ca0d15f..98948b2cd2533 100644 --- a/.prettierignore +++ b/.prettierignore @@ -24,4 +24,5 @@ node_modules/ /solution/2200-2299/2230.The Users That Are Eligible for Discount/Solution.sql /solution/2200-2299/2252.Dynamic Pivoting of a Table/Solution.sql /solution/2200-2299/2253.Dynamic Unpivoting of a Table/Solution.sql -/solution/3100-3199/3150.Invalid Tweets II/Solution.sql \ No newline at end of file +/solution/3100-3199/3150.Invalid Tweets II/Solution.sql +/solution/3100-3199/3198.Find Cities in Each State/Solution.sql \ No newline at end of file diff --git a/solution/3100-3199/3198.Find Cities in Each State/README.md b/solution/3100-3199/3198.Find Cities in Each State/README.md new file mode 100644 index 0000000000000..d340f3d27c5d0 --- /dev/null +++ b/solution/3100-3199/3198.Find Cities in Each State/README.md @@ -0,0 +1,128 @@ +--- +comments: true +difficulty: 简单 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md +--- + + + +# [3198. 查找每个州的城市 🔒](https://leetcode.cn/problems/find-cities-in-each-state) + +[English Version](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) + +## 题目描述 + + + +

表:cities

+ +
++-------------+---------+
+| Column Name | Type    | 
++-------------+---------+
+| state       | varchar |
+| city        | varchar |
++-------------+---------+
+(state, city) 是这张表的主键(有不同值的列的组合)。
+这张表的每一行包含州名和其中的城市名。
+
+ +

编写一个解决方案来 查找每个州的所有城市,并将它们组合成 一个逗号分隔 的字符串。

+ +

返回结果表以 state 升序 排序。

+ +

结果格式如下所示。

+ +

 

+ +

示例:

+ +
+

输入:

+ +

cities 表:

+ +
++-------------+---------------+
+| state       | city          |
++-------------+---------------+
+| California  | Los Angeles   |
+| California  | San Francisco |
+| California  | San Diego     |
+| Texas       | Houston       |
+| Texas       | Austin        |
+| Texas       | Dallas        |
+| New York    | New York City |
+| New York    | Buffalo       |
+| New York    | Rochester     |
++-------------+---------------+
+
+ +

输出:

+ +
++-------------+---------------------------------------+
+| state       | cities                                |
++-------------+---------------------------------------+
+| California  | Los Angeles, San Diego, San Francisco |
+| New York    | Buffalo, New York City, Rochester     |
+| Texas       | Austin, Dallas, Houston               |
++-------------+---------------------------------------+
+
+ +

解释:

+ + + +

注意:输出表以州名升序排序。

+
+ + + +## 解法 + + + +### 方法一:分组聚合 + +我们可以先按照 `state` 字段进行分组,然后对每个分组内的 `city` 字段进行排序,最后使用 `GROUP_CONCAT` 函数将排序后的城市名连接成一个逗号分隔的字符串。 + + + +#### MySQL + +```sql +# Write your MySQL query statement below +SELECT + state, + GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') cities +FROM cities +GROUP BY 1 +ORDER BY 1; +``` + +#### Pandas + +```python +import pandas as pd + + +def find_cities(cities: pd.DataFrame) -> pd.DataFrame: + result = ( + cities.groupby("state")["city"] + .apply(lambda x: ", ".join(sorted(x))) + .reset_index() + ) + result.columns = ["state", "cities"] + return result +``` + + + + + + diff --git a/solution/3100-3199/3198.Find Cities in Each State/README_EN.md b/solution/3100-3199/3198.Find Cities in Each State/README_EN.md new file mode 100644 index 0000000000000..996bb54bffad1 --- /dev/null +++ b/solution/3100-3199/3198.Find Cities in Each State/README_EN.md @@ -0,0 +1,127 @@ +--- +comments: true +difficulty: Easy +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md +--- + + + +# [3198. Find Cities in Each State 🔒](https://leetcode.com/problems/find-cities-in-each-state) + +[中文文档](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) + +## Description + + + +

Table: cities

+ +
++-------------+---------+
+| Column Name | Type    | 
++-------------+---------+
+| state       | varchar |
+| city        | varchar |
++-------------+---------+
+(state, city) is the primary key (combination of columns with unique values) for this table.
+Each row of this table contains the state name and the city name within that state.
+
+ +

Write a solution to find all the cities in each state and combine them into a single comma-separated string.

+ +

Return the result table ordered by state in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

cities table:

+ +
++-------------+---------------+
+| state       | city          |
++-------------+---------------+
+| California  | Los Angeles   |
+| California  | San Francisco |
+| California  | San Diego     |
+| Texas       | Houston       |
+| Texas       | Austin        |
+| Texas       | Dallas        |
+| New York    | New York City |
+| New York    | Buffalo       |
+| New York    | Rochester     |
++-------------+---------------+
+
+ +

Output:

+ +
++-------------+---------------------------------------+
+| state       | cities                                |
++-------------+---------------------------------------+
+| California  | Los Angeles, San Diego, San Francisco |
+| New York    | Buffalo, New York City, Rochester     |
+| Texas       | Austin, Dallas, Houston               |
++-------------+---------------------------------------+
+
+ +

Explanation:

+ + + +

Note: The output table is ordered by the state name in ascending order.

+
+ + + +## Solutions + + + +### Solution 1: Grouping and Aggregation + +We can first group by the `state` field, then sort the `city` field within each group, and finally use the `GROUP_CONCAT` function to concatenate the sorted city names into a comma-separated string. + + + +#### MySQL + +```sql +# Write your MySQL query statement below +SELECT + state, + GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') cities +FROM cities +GROUP BY 1 +ORDER BY 1; +``` + +#### Pandas + +```python +import pandas as pd + + +def find_cities(cities: pd.DataFrame) -> pd.DataFrame: + result = ( + cities.groupby("state")["city"] + .apply(lambda x: ", ".join(sorted(x))) + .reset_index() + ) + result.columns = ["state", "cities"] + return result +``` + + + + + + diff --git a/solution/3100-3199/3198.Find Cities in Each State/Solution.py b/solution/3100-3199/3198.Find Cities in Each State/Solution.py new file mode 100644 index 0000000000000..6feb669ac2c73 --- /dev/null +++ b/solution/3100-3199/3198.Find Cities in Each State/Solution.py @@ -0,0 +1,11 @@ +import pandas as pd + + +def find_cities(cities: pd.DataFrame) -> pd.DataFrame: + result = ( + cities.groupby("state")["city"] + .apply(lambda x: ", ".join(sorted(x))) + .reset_index() + ) + result.columns = ["state", "cities"] + return result diff --git a/solution/3100-3199/3198.Find Cities in Each State/Solution.sql b/solution/3100-3199/3198.Find Cities in Each State/Solution.sql new file mode 100644 index 0000000000000..1e8dbc7d8633e --- /dev/null +++ b/solution/3100-3199/3198.Find Cities in Each State/Solution.sql @@ -0,0 +1,7 @@ +# Write your MySQL query statement below +SELECT + state, + GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') cities +FROM cities +GROUP BY 1 +ORDER BY 1; \ No newline at end of file diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 969dd0a0f92ec..bf49209c2a1ba 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -284,6 +284,7 @@ | 3172 | [第二天验证](/solution/3100-3199/3172.Second%20Day%20Verification/README.md) | `数据库` | 简单 | 🔒 | | 3182 | [查找得分最高的学生](/solution/3100-3199/3182.Find%20Top%20Scoring%20Students/README.md) | `数据库` | 中等 | 🔒 | | 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 | +| 3198 | [查找每个州的城市](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) | | 简单 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index b9febbe7756e1..d63d9210ef6c3 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -282,6 +282,7 @@ Press Control + F(or Command + F on | 3172 | [Second Day Verification](/solution/3100-3199/3172.Second%20Day%20Verification/README_EN.md) | `Database` | Easy | 🔒 | | 3182 | [Find Top Scoring Students](/solution/3100-3199/3182.Find%20Top%20Scoring%20Students/README_EN.md) | `Database` | Medium | 🔒 | | 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README_EN.md) | | Hard | 🔒 | +| 3198 | [Find Cities in Each State](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) | | Easy | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index a9b1395672282..65a435b4ef2ed 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3208,6 +3208,7 @@ | 3195 | [包含所有 1 的最小矩形面积 I](/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/README.md) | | 中等 | 第 403 场周赛 | | 3196 | [最大化子数组的总成本](/solution/3100-3199/3196.Maximize%20Total%20Cost%20of%20Alternating%20Subarrays/README.md) | | 中等 | 第 403 场周赛 | | 3197 | [包含所有 1 的最小矩形面积 II](/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/README.md) | | 困难 | 第 403 场周赛 | +| 3198 | [查找每个州的城市](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) | | 简单 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 00ccaf1eccdc6..8f51d1c735740 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3206,6 +3206,7 @@ Press Control + F(or Command + F on | 3195 | [Find the Minimum Area to Cover All Ones I](/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/README_EN.md) | | Medium | Weekly Contest 403 | | 3196 | [Maximize Total Cost of Alternating Subarrays](/solution/3100-3199/3196.Maximize%20Total%20Cost%20of%20Alternating%20Subarrays/README_EN.md) | | Medium | Weekly Contest 403 | | 3197 | [Find the Minimum Area to Cover All Ones II](/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/README_EN.md) | | Hard | Weekly Contest 403 | +| 3198 | [Find Cities in Each State](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) | | Easy | 🔒 | ## Copyright