Skip to content

feat: add solutions to lc problem: No.3198 #3164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
/solution/3100-3199/3150.Invalid Tweets II/Solution.sql
/solution/3100-3199/3198.Find Cities in Each State/Solution.sql
128 changes: 128 additions & 0 deletions solution/3100-3199/3198.Find Cities in Each State/README.md
Original file line number Diff line number Diff line change
@@ -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
---

<!-- problem:start -->

# [3198. 查找每个州的城市 🔒](https://leetcode.cn/problems/find-cities-in-each-state)

[English Version](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md)

## 题目描述

<!-- description:start -->

<p>表:<code>cities</code></p>

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

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

<p>返回结果表以&nbsp;<code>state</code> <strong>升序&nbsp;</strong>排序。</p>

<p>结果格式如下所示。</p>

<p>&nbsp;</p>

<p><strong class="example">示例:</strong></p>

<div class="example-block">
<p><strong>输入:</strong></p>

<p>cities 表:</p>

<pre class="example-io">
+-------------+---------------+
| 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 |
+-------------+---------------+
</pre>

<p><strong>输出:</strong></p>

<pre class="example-io">
+-------------+---------------------------------------+
| state | cities |
+-------------+---------------------------------------+
| California | Los Angeles, San Diego, San Francisco |
| New York | Buffalo, New York City, Rochester |
| Texas | Austin, Dallas, Houston |
+-------------+---------------------------------------+
</pre>

<p><strong>解释:</strong></p>

<ul>
<li><strong>California:</strong>所有城市 ("Los Angeles", "San Diego", "San Francisco") 以逗号分隔的字符串列出。</li>
<li><strong>New York:</strong>所有城市 ("Buffalo", "New York City", "Rochester") 以逗号分隔的字符串列出。</li>
<li><strong>Texas:</strong>所有城市 ("Austin", "Dallas", "Houston") 以逗号分隔的字符串列出。</li>
</ul>

<p><b>注意:</b>输出表以州名升序排序。</p>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:分组聚合

我们可以先按照 `state` 字段进行分组,然后对每个分组内的 `city` 字段进行排序,最后使用 `GROUP_CONCAT` 函数将排序后的城市名连接成一个逗号分隔的字符串。

<!-- tabs:start -->

#### 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
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
127 changes: 127 additions & 0 deletions solution/3100-3199/3198.Find Cities in Each State/README_EN.md
Original file line number Diff line number Diff line change
@@ -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
---

<!-- problem:start -->

# [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

<!-- description:start -->

<p>Table: <code>cities</code></p>

<pre>
+-------------+---------+
| 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.
</pre>

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

<p>Return <em>the result table ordered by</em> <code>state</code> <em>in <strong>ascending</strong> order</em>.</p>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p>cities table:</p>

<pre class="example-io">
+-------------+---------------+
| 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 |
+-------------+---------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+-------------+---------------------------------------+
| state | cities |
+-------------+---------------------------------------+
| California | Los Angeles, San Diego, San Francisco |
| New York | Buffalo, New York City, Rochester |
| Texas | Austin, Dallas, Houston |
+-------------+---------------------------------------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li><strong>California:</strong> All cities (&quot;Los Angeles&quot;, &quot;San Diego&quot;, &quot;San Francisco&quot;) are listed in a comma-separated string.</li>
<li><strong>New York:</strong> All cities (&quot;Buffalo&quot;, &quot;New York City&quot;, &quot;Rochester&quot;) are listed in a comma-separated string.</li>
<li><strong>Texas:</strong> All cities (&quot;Austin&quot;, &quot;Dallas&quot;, &quot;Houston&quot;) are listed in a comma-separated string.</li>
</ul>

<p><strong>Note:</strong> The output table is ordered by the state name in ascending order.</p>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### 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.

<!-- tabs:start -->

#### 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
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
11 changes: 11 additions & 0 deletions solution/3100-3199/3198.Find Cities in Each State/Solution.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | 简单 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/DATABASE_README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> 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

Expand Down
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | 简单 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> 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

Expand Down
Loading