595. 大的国家
595. 大的国家
题目
Table: World
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | bigint |
+-------------+---------+
name is the primary key (column with unique values) for this table.
Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value.
A country is big if:
- it has an area of at least three million (i.e.,
3000000 km2
), or - it has a population of at least twenty-five million (i.e.,
25000000
).
Write a solution to find the name, population, and area of the big countries.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
World table:
+-------------+-----------+---------+------------+--------------+ | name | continent | area | population | gdp | +-------------+-----------+---------+------------+--------------+ | Afghanistan | Asia | 652230 | 25500100 | 20343000000 | | Albania | Europe | 28748 | 2831741 | 12960000000 | | Algeria | Africa | 2381741 | 37100000 | 188681000000 | | Andorra | Europe | 468 | 78115 | 3712000000 | | Angola | Africa | 1246700 | 20609294 | 100990000000 | +-------------+-----------+---------+------------+--------------+
Output:
+-------------+------------+---------+ | name | population | area | +-------------+------------+---------+ | Afghanistan | 25500100 | 652230 | | Algeria | 37100000 | 2381741 | +-------------+------------+---------+
题目大意
World
表:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | bigint |
+-------------+---------+
name 是该表的主键(具有唯一值的列)。
这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。
如果一个国家满足下述两个条件之一,则认为该国是 大国 :
- 面积至少为 300 万平方公里(即,
3000000 km2
),或者 - 人口至少为 2500 万(即
25000000
)
编写解决方案找出 大国 的国家名称、人口和面积。
按 任意顺序 返回结果表。
返回结果格式如下例所示。
示例:
输入:
World 表:
World table:
+-------------+-----------+---------+------------+--------------+ | name | continent | area | population | gdp | +-------------+-----------+---------+------------+--------------+ | Afghanistan | Asia | 652230 | 25500100 | 20343000000 | | Albania | Europe | 28748 | 2831741 | 12960000000 | | Algeria | Africa | 2381741 | 37100000 | 188681000000 | | Andorra | Europe | 468 | 78115 | 3712000000 | | Angola | Africa | 1246700 | 20609294 | 100990000000 | +-------------+-----------+---------+------------+--------------+
输出:
+-------------+------------+---------+ | name | population | area | +-------------+------------+---------+ | Afghanistan | 25500100 | 652230 | | Algeria | 37100000 | 2381741 | +-------------+------------+---------+
解题思路
MySQL
返回字段:
SELECT
指定要返回的字段:name
(国家名称)、population
(人口)、area
(面积)。FROM World
指定数据来自World
表。筛选条件:
area > 3000000
:筛选面积大于 3,000,000 的国家。population > 25000000
:筛选人口超过 25,000,000 的国家。WHERE
子句的两个条件用OR
连接,表示满足任何一个都可以。
复杂度分析
- 时间复杂度:
O(n)
,假设表中有n
条记录,查询的时间复杂度为O(n)
,因为需要遍历所有记录来评估条件。 - 空间复杂度:SQL 查询本身不占用额外空间,返回的结果占用的空间与满足条件的记录数成正比。
Pandas
- 加载数据:将数据加载到一个 Pandas
DataFrame
中; - 筛选条件:使用 Pandas 的布尔索引筛选出满足以下条件的记录:
world['area'] >= 3000000
- 或者
world['population'] >= 25000000
- 选择列:筛选结果中保留需要的字段:
name
,population
, 和area
。
代码
MySQL
SELECT name, population, area
FROM World
WHERE area > 3000000 OR population > 25000000;
Pandas
import pandas as pd
def big_countries(world: pd.DataFrame) -> pd.DataFrame:
# 转换为 DataFrame,筛选符合条件的记录
return world[
(world['area'] >= 3000000) | (world['population'] >= 25000000)
][['name', 'population', 'area']]