Skip to content

Commit afc8673

Browse files
authored
feat: add solutions to lc problem: No.3198 (#3164)
No.3198.Find Cities in Each State
1 parent 5be8a6f commit afc8673

File tree

9 files changed

+279
-1
lines changed

9 files changed

+279
-1
lines changed

.prettierignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ node_modules/
2424
/solution/2200-2299/2230.The Users That Are Eligible for Discount/Solution.sql
2525
/solution/2200-2299/2252.Dynamic Pivoting of a Table/Solution.sql
2626
/solution/2200-2299/2253.Dynamic Unpivoting of a Table/Solution.sql
27-
/solution/3100-3199/3150.Invalid Tweets II/Solution.sql
27+
/solution/3100-3199/3150.Invalid Tweets II/Solution.sql
28+
/solution/3100-3199/3198.Find Cities in Each State/Solution.sql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3198. 查找每个州的城市 🔒](https://leetcode.cn/problems/find-cities-in-each-state)
10+
11+
[English Version](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>表:<code>cities</code></p>
18+
19+
<pre>
20+
+-------------+---------+
21+
| Column Name | Type |
22+
+-------------+---------+
23+
| state | varchar |
24+
| city | varchar |
25+
+-------------+---------+
26+
(state, city) 是这张表的主键(有不同值的列的组合)。
27+
这张表的每一行包含州名和其中的城市名。
28+
</pre>
29+
30+
<p>编写一个解决方案来 <strong>查找每个州的所有城市</strong>,并将它们组合成 <strong>一个逗号分隔</strong> 的字符串。</p>
31+
32+
<p>返回结果表以&nbsp;<code>state</code> <strong>升序&nbsp;</strong>排序。</p>
33+
34+
<p>结果格式如下所示。</p>
35+
36+
<p>&nbsp;</p>
37+
38+
<p><strong class="example">示例:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>输入:</strong></p>
42+
43+
<p>cities 表:</p>
44+
45+
<pre class="example-io">
46+
+-------------+---------------+
47+
| state | city |
48+
+-------------+---------------+
49+
| California | Los Angeles |
50+
| California | San Francisco |
51+
| California | San Diego |
52+
| Texas | Houston |
53+
| Texas | Austin |
54+
| Texas | Dallas |
55+
| New York | New York City |
56+
| New York | Buffalo |
57+
| New York | Rochester |
58+
+-------------+---------------+
59+
</pre>
60+
61+
<p><strong>输出:</strong></p>
62+
63+
<pre class="example-io">
64+
+-------------+---------------------------------------+
65+
| state | cities |
66+
+-------------+---------------------------------------+
67+
| California | Los Angeles, San Diego, San Francisco |
68+
| New York | Buffalo, New York City, Rochester |
69+
| Texas | Austin, Dallas, Houston |
70+
+-------------+---------------------------------------+
71+
</pre>
72+
73+
<p><strong>解释:</strong></p>
74+
75+
<ul>
76+
<li><strong>California:</strong>所有城市 ("Los Angeles", "San Diego", "San Francisco") 以逗号分隔的字符串列出。</li>
77+
<li><strong>New York:</strong>所有城市 ("Buffalo", "New York City", "Rochester") 以逗号分隔的字符串列出。</li>
78+
<li><strong>Texas:</strong>所有城市 ("Austin", "Dallas", "Houston") 以逗号分隔的字符串列出。</li>
79+
</ul>
80+
81+
<p><b>注意:</b>输出表以州名升序排序。</p>
82+
</div>
83+
84+
<!-- description:end -->
85+
86+
## 解法
87+
88+
<!-- solution:start -->
89+
90+
### 方法一:分组聚合
91+
92+
我们可以先按照 `state` 字段进行分组,然后对每个分组内的 `city` 字段进行排序,最后使用 `GROUP_CONCAT` 函数将排序后的城市名连接成一个逗号分隔的字符串。
93+
94+
<!-- tabs:start -->
95+
96+
#### MySQL
97+
98+
```sql
99+
# Write your MySQL query statement below
100+
SELECT
101+
state,
102+
GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') cities
103+
FROM cities
104+
GROUP BY 1
105+
ORDER BY 1;
106+
```
107+
108+
#### Pandas
109+
110+
```python
111+
import pandas as pd
112+
113+
114+
def find_cities(cities: pd.DataFrame) -> pd.DataFrame:
115+
result = (
116+
cities.groupby("state")["city"]
117+
.apply(lambda x: ", ".join(sorted(x)))
118+
.reset_index()
119+
)
120+
result.columns = ["state", "cities"]
121+
return result
122+
```
123+
124+
<!-- tabs:end -->
125+
126+
<!-- solution:end -->
127+
128+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3198. Find Cities in Each State 🔒](https://leetcode.com/problems/find-cities-in-each-state)
10+
11+
[中文文档](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>Table: <code>cities</code></p>
18+
19+
<pre>
20+
+-------------+---------+
21+
| Column Name | Type |
22+
+-------------+---------+
23+
| state | varchar |
24+
| city | varchar |
25+
+-------------+---------+
26+
(state, city) is the primary key (combination of columns with unique values) for this table.
27+
Each row of this table contains the state name and the city name within that state.
28+
</pre>
29+
30+
<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>
31+
32+
<p>Return <em>the result table ordered by</em> <code>state</code> <em>in <strong>ascending</strong> order</em>.</p>
33+
34+
<p>The result format is in the following example.</p>
35+
36+
<p>&nbsp;</p>
37+
<p><strong class="example">Example:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>Input:</strong></p>
41+
42+
<p>cities table:</p>
43+
44+
<pre class="example-io">
45+
+-------------+---------------+
46+
| state | city |
47+
+-------------+---------------+
48+
| California | Los Angeles |
49+
| California | San Francisco |
50+
| California | San Diego |
51+
| Texas | Houston |
52+
| Texas | Austin |
53+
| Texas | Dallas |
54+
| New York | New York City |
55+
| New York | Buffalo |
56+
| New York | Rochester |
57+
+-------------+---------------+
58+
</pre>
59+
60+
<p><strong>Output:</strong></p>
61+
62+
<pre class="example-io">
63+
+-------------+---------------------------------------+
64+
| state | cities |
65+
+-------------+---------------------------------------+
66+
| California | Los Angeles, San Diego, San Francisco |
67+
| New York | Buffalo, New York City, Rochester |
68+
| Texas | Austin, Dallas, Houston |
69+
+-------------+---------------------------------------+
70+
</pre>
71+
72+
<p><strong>Explanation:</strong></p>
73+
74+
<ul>
75+
<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>
76+
<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>
77+
<li><strong>Texas:</strong> All cities (&quot;Austin&quot;, &quot;Dallas&quot;, &quot;Houston&quot;) are listed in a comma-separated string.</li>
78+
</ul>
79+
80+
<p><strong>Note:</strong> The output table is ordered by the state name in ascending order.</p>
81+
</div>
82+
83+
<!-- description:end -->
84+
85+
## Solutions
86+
87+
<!-- solution:start -->
88+
89+
### Solution 1: Grouping and Aggregation
90+
91+
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.
92+
93+
<!-- tabs:start -->
94+
95+
#### MySQL
96+
97+
```sql
98+
# Write your MySQL query statement below
99+
SELECT
100+
state,
101+
GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') cities
102+
FROM cities
103+
GROUP BY 1
104+
ORDER BY 1;
105+
```
106+
107+
#### Pandas
108+
109+
```python
110+
import pandas as pd
111+
112+
113+
def find_cities(cities: pd.DataFrame) -> pd.DataFrame:
114+
result = (
115+
cities.groupby("state")["city"]
116+
.apply(lambda x: ", ".join(sorted(x)))
117+
.reset_index()
118+
)
119+
result.columns = ["state", "cities"]
120+
return result
121+
```
122+
123+
<!-- tabs:end -->
124+
125+
<!-- solution:end -->
126+
127+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pandas as pd
2+
3+
4+
def find_cities(cities: pd.DataFrame) -> pd.DataFrame:
5+
result = (
6+
cities.groupby("state")["city"]
7+
.apply(lambda x: ", ".join(sorted(x)))
8+
.reset_index()
9+
)
10+
result.columns = ["state", "cities"]
11+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
state,
4+
GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') cities
5+
FROM cities
6+
GROUP BY 1
7+
ORDER BY 1;

solution/DATABASE_README.md

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
| 3172 | [第二天验证](/solution/3100-3199/3172.Second%20Day%20Verification/README.md) | `数据库` | 简单 | 🔒 |
285285
| 3182 | [查找得分最高的学生](/solution/3100-3199/3182.Find%20Top%20Scoring%20Students/README.md) | `数据库` | 中等 | 🔒 |
286286
| 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 |
287+
| 3198 | [查找每个州的城市](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) | | 简单 | 🔒 |
287288

288289
## 版权
289290

solution/DATABASE_README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
282282
| 3172 | [Second Day Verification](/solution/3100-3199/3172.Second%20Day%20Verification/README_EN.md) | `Database` | Easy | 🔒 |
283283
| 3182 | [Find Top Scoring Students](/solution/3100-3199/3182.Find%20Top%20Scoring%20Students/README_EN.md) | `Database` | Medium | 🔒 |
284284
| 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README_EN.md) | | Hard | 🔒 |
285+
| 3198 | [Find Cities in Each State](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) | | Easy | 🔒 |
285286

286287
## Copyright
287288

solution/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3208,6 +3208,7 @@
32083208
| 3195 | [包含所有 1 的最小矩形面积 I](/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/README.md) | | 中等 | 第 403 场周赛 |
32093209
| 3196 | [最大化子数组的总成本](/solution/3100-3199/3196.Maximize%20Total%20Cost%20of%20Alternating%20Subarrays/README.md) | | 中等 | 第 403 场周赛 |
32103210
| 3197 | [包含所有 1 的最小矩形面积 II](/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/README.md) | | 困难 | 第 403 场周赛 |
3211+
| 3198 | [查找每个州的城市](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) | | 简单 | 🔒 |
32113212

32123213
## 版权
32133214

solution/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
32063206
| 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 |
32073207
| 3196 | [Maximize Total Cost of Alternating Subarrays](/solution/3100-3199/3196.Maximize%20Total%20Cost%20of%20Alternating%20Subarrays/README_EN.md) | | Medium | Weekly Contest 403 |
32083208
| 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 |
3209+
| 3198 | [Find Cities in Each State](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) | | Easy | 🔒 |
32093210

32103211
## Copyright
32113212

0 commit comments

Comments
 (0)