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:
3000000 km2), or25000000).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 | +-------------+------------+---------+
The task requires selecting names, populations, and areas of countries based on certain criteria: whether the area is at least 3 million or the population is at least 25 million. We can achieve this using a SQL query that utilizes the WHERE clause to filter the specified conditions.
This SQL query selects the name, population, and area columns from the World table. It uses a WHERE clause to filter the records where either the area is greater than or equal to 3 million or the population is greater than or equal to 25 million. The OR operator is used to check if any of the conditions are met. The result consists of records that satisfy at least one of these conditions.
Time Complexity: O(n) where n is the number of rows in the World table, as all rows potentially need to be scanned once.
Space Complexity: O(n) due to storing the result set that meets the criteria.
In the iterative filtering approach, we simulate the results of the SQL query using an iterative process in various programming languages. We loop through each record, check if a record satisfies either of the conditions, and collect the results accordingly. This is done using simple loops and condition checks.
This Python function takes a list of dictionaries where each dictionary represents a country's information. It iterates over the list and checks if the 'area' is at least 3 million or the 'population' is at least 25 million. If a condition is satisfied, the country is added to the result list.
JavaScript
Java
C#
C++
Time Complexity: O(n), where n is the number of countries.
Space Complexity: O(n), to store the resulting list of countries that meet the criteria.
| Approach | Complexity |
|---|---|
| SQL Query for Big Countries | Time Complexity: O(n) where n is the number of rows in the World table, as all rows potentially need to be scanned once. |
| Iterative Filtering Approach | Time Complexity: O(n), where n is the number of countries. |
LeetCode Big Countries Solution Explained - Java • Nick White • 6,595 views views
Watch 9 more video solutions →Practice Big Countries with our built-in code editor and test cases.
Practice on FleetCode