Skip to content

Commit 38a8de4

Browse files
committed
Create readme.md
1 parent e2a869d commit 38a8de4

File tree

1 file changed

+142
-0
lines changed
  • LeetCode SQL 50 Solution/595. Big Countries

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
Below is the structured README.md snippet for **LeetCode 595: Big Countries**, including the problem statement, example, solution approaches (SQL and Pandas), file structure, and useful links.
2+
3+
---
4+
5+
# **595. Big Countries**
6+
7+
## **Problem Statement**
8+
You are given a table `World` that contains information about countries.
9+
10+
### **World Table**
11+
```
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| name | varchar |
16+
| continent | varchar |
17+
| area | int |
18+
| population | int |
19+
| gdp | bigint |
20+
+-------------+---------+
21+
```
22+
- `name` is the **primary key**.
23+
- Each row contains:
24+
- `name`: Name of the country.
25+
- `continent`: Continent the country belongs to.
26+
- `area`: Area of the country (in km²).
27+
- `population`: Population of the country.
28+
- `gdp`: GDP of the country.
29+
30+
### **Task:**
31+
A country is considered **big** if:
32+
- It has an **area** of at least **3,000,000 km²**, **or**
33+
- It has a **population** of at least **25,000,000**.
34+
35+
Write a solution to find the **name**, **population**, and **area** of the big countries.
36+
37+
Return the result table in **any order**.
38+
39+
---
40+
41+
## **Example 1:**
42+
43+
### **Input:**
44+
#### **World Table**
45+
```
46+
+-------------+-----------+---------+------------+--------------+
47+
| name | continent | area | population | gdp |
48+
+-------------+-----------+---------+------------+--------------+
49+
| Afghanistan | Asia | 652230 | 25500100 | 20343000000 |
50+
| Albania | Europe | 28748 | 2831741 | 12960000000 |
51+
| Algeria | Africa | 2381741 | 37100000 | 188681000000 |
52+
| Andorra | Europe | 468 | 78115 | 3712000000 |
53+
| Angola | Africa | 1246700 | 20609294 | 100990000000 |
54+
+-------------+-----------+---------+------------+--------------+
55+
```
56+
57+
### **Output:**
58+
```
59+
+-------------+------------+---------+
60+
| name | population | area |
61+
+-------------+------------+---------+
62+
| Afghanistan | 25500100 | 652230 |
63+
| Algeria | 37100000 | 2381741 |
64+
+-------------+------------+---------+
65+
```
66+
67+
### **Explanation:**
68+
- **Afghanistan** is not big by area (652,230 < 3,000,000) but is big by population (25,500,100 ≥ 25,000,000).
69+
- **Algeria** is big by population (37,100,000 ≥ 25,000,000), even though its area (2,381,741) is less than 3,000,000.
70+
- The other countries do not meet either condition.
71+
72+
---
73+
74+
## **Solution Approaches**
75+
76+
### **SQL Solution (Using UNION)**
77+
```sql
78+
SELECT name, population, area
79+
FROM World
80+
WHERE area >= 3000000
81+
UNION
82+
SELECT name, population, area
83+
FROM World
84+
WHERE population >= 25000000;
85+
```
86+
**Explanation:**
87+
- The first `SELECT` returns countries with an area of at least 3,000,000 km².
88+
- The second `SELECT` returns countries with a population of at least 25,000,000.
89+
- `UNION` combines these two result sets, ensuring unique rows.
90+
91+
---
92+
93+
### **SQL Alternative (Using OR)**
94+
```sql
95+
SELECT name, population, area
96+
FROM World
97+
WHERE area >= 3000000 OR population >= 25000000;
98+
```
99+
**Explanation:**
100+
- This query uses a single `SELECT` statement with an `OR` condition to capture countries that meet either criterion.
101+
102+
---
103+
104+
### **Pandas Solution**
105+
```python
106+
import pandas as pd
107+
108+
def big_countries(world: pd.DataFrame) -> pd.DataFrame:
109+
# Filter countries that are considered big by either area or population
110+
result = world[(world['area'] >= 3000000) | (world['population'] >= 25000000)][['name', 'population', 'area']]
111+
return result
112+
113+
# Example usage:
114+
# world_df = pd.read_csv('world.csv')
115+
# print(big_countries(world_df))
116+
```
117+
**Explanation:**
118+
- The Pandas solution filters the DataFrame based on the condition that `area` is at least 3,000,000 or `population` is at least 25,000,000.
119+
- It then returns the columns `name`, `population`, and `area`.
120+
121+
---
122+
123+
## **File Structure**
124+
```
125+
LeetCode595/
126+
├── problem_statement.md # Contains the problem description and constraints.
127+
├── sql_union_solution.sql # SQL solution using UNION.
128+
├── sql_or_solution.sql # SQL alternative solution using OR.
129+
├── pandas_solution.py # Pandas solution for Python users.
130+
├── README.md # Overview of the problem and solutions.
131+
```
132+
133+
---
134+
135+
## **Useful Links**
136+
- [LeetCode Problem 595](https://leetcode.com/problems/big-countries/)
137+
- [SQL WHERE Clause](https://www.w3schools.com/sql/sql_where.asp)
138+
- [Pandas Documentation](https://pandas.pydata.org/docs/)
139+
140+
---
141+
142+
This structured format provides a comprehensive overview of the problem along with multiple solution approaches. Happy coding! 🚀

0 commit comments

Comments
 (0)