Skip to content

Commit e7be90f

Browse files
authored
Merge pull request #74 from iamAntimPal/Branch-1
Branch 1
2 parents 18d6603 + e657134 commit e7be90f

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

LeetCode SQL 50 Solution/197. Rising Temperature/Rising Temperature.py

Whitespace-only changes.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
2+
# **197. Rising Temperature**
3+
4+
## **Problem Statement**
5+
You are given a table called `Weather`, which contains daily temperature records.
6+
7+
### **Weather Table**
8+
```
9+
+---------------+---------+
10+
| Column Name | Type |
11+
+---------------+---------+
12+
| id | int |
13+
| recordDate | date |
14+
| temperature | int |
15+
+---------------+---------+
16+
```
17+
- `id` is the **primary key**.
18+
- Each row contains:
19+
- `recordDate`: The **date** of the temperature record.
20+
- `temperature`: The **temperature recorded** on that date.
21+
22+
### **Task:**
23+
Find all `id`s where the **temperature** is **higher** than the **previous day's temperature**.
24+
25+
---
26+
27+
## **Example 1:**
28+
### **Input:**
29+
#### **Weather Table**
30+
```
31+
+----+------------+-------------+
32+
| id | recordDate | temperature |
33+
+----+------------+-------------+
34+
| 1 | 2024-08-01 | 30 |
35+
| 2 | 2024-08-02 | 32 |
36+
| 3 | 2024-08-03 | 31 |
37+
| 4 | 2024-08-04 | 35 |
38+
| 5 | 2024-08-05 | 36 |
39+
+----+------------+-------------+
40+
```
41+
### **Output:**
42+
```
43+
+----+
44+
| id |
45+
+----+
46+
| 2 |
47+
| 4 |
48+
| 5 |
49+
+----+
50+
```
51+
### **Explanation:**
52+
- `id = 2`: `32 > 30` (08-02 > 08-01 ✅)
53+
- `id = 3`: `31 < 32` (Skipped ❌)
54+
- `id = 4`: `35 > 31` (08-04 > 08-03 ✅)
55+
- `id = 5`: `36 > 35` (08-05 > 08-04 ✅)
56+
57+
---
58+
59+
## **Solution Approaches**
60+
61+
### **SQL Solution (Using `LAG()` Window Function)**
62+
```sql
63+
WITH PreviousWeatherData AS
64+
(
65+
SELECT
66+
id,
67+
recordDate,
68+
temperature,
69+
LAG(temperature, 1) OVER (ORDER BY recordDate) AS PreviousTemperature,
70+
LAG(recordDate, 1) OVER (ORDER BY recordDate) AS PreviousRecordDate
71+
FROM
72+
Weather
73+
)
74+
SELECT
75+
id
76+
FROM
77+
PreviousWeatherData
78+
WHERE
79+
temperature > PreviousTemperature
80+
AND
81+
recordDate = DATE_ADD(PreviousRecordDate, INTERVAL 1 DAY);
82+
```
83+
**Explanation:**
84+
- We use `LAG()` to fetch:
85+
- The **previous day's temperature**.
86+
- The **previous day's date**.
87+
- The `WHERE` clause filters rows where:
88+
- The **temperature is higher than the previous day**.
89+
- The **date difference is exactly 1 day**.
90+
91+
---
92+
93+
### **SQL Solution (Using Self Join)**
94+
```sql
95+
SELECT w1.id
96+
FROM Weather w1
97+
JOIN Weather w2
98+
ON DATEDIFF(w1.recordDate, w2.recordDate) = 1
99+
AND w1.temperature > w2.temperature;
100+
```
101+
**Explanation:**
102+
- We **self-join** the `Weather` table.
103+
- The condition `DATEDIFF(w1.recordDate, w2.recordDate) = 1` ensures:
104+
- We are comparing **consecutive days**.
105+
- The condition `w1.temperature > w2.temperature` ensures:
106+
- We select days where the **temperature increased**.
107+
108+
---
109+
110+
### **Pandas Solution**
111+
```python
112+
import pandas as pd
113+
114+
def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
115+
weather.sort_values(by="recordDate", inplace=True)
116+
weather["PreviousTemp"] = weather["temperature"].shift(1)
117+
weather["PreviousDate"] = weather["recordDate"].shift(1)
118+
119+
result = weather[
120+
(weather["temperature"] > weather["PreviousTemp"]) &
121+
((weather["recordDate"] - weather["PreviousDate"]).dt.days == 1)
122+
]
123+
124+
return result[["id"]]
125+
```
126+
**Explanation:**
127+
- We **sort** by `recordDate`.
128+
- We **shift** the temperature and date to get the previous day's values.
129+
- We **filter** where:
130+
- Temperature **increased**.
131+
- Date difference is **1 day**.
132+
133+
---
134+
135+
## **File Structure**
136+
```
137+
📂 LeetCode197
138+
│── 📜 problem_statement.md
139+
│── 📜 sql_lag_solution.sql
140+
│── 📜 sql_self_join_solution.sql
141+
│── 📜 pandas_solution.py
142+
│── 📜 README.md
143+
```
144+
- `problem_statement.md` → Contains the problem description.
145+
- `sql_lag_solution.sql` → Contains the SQL solution using **LAG()**.
146+
- `sql_self_join_solution.sql` → Contains the SQL solution using **Self Join**.
147+
- `pandas_solution.py` → Contains the Pandas solution.
148+
- `README.md` → Provides an overview of the problem and solutions.
149+
150+
---
151+
152+
## **Useful Links**
153+
- [LeetCode Problem 197](https://leetcode.com/problems/rising-temperature/)
154+
- [SQL LAG() Function](https://www.w3schools.com/sql/sql_ref_window_functions.asp)
155+
- [SQL JOIN](https://www.w3schools.com/sql/sql_join.asp)
156+
- [Pandas shift()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.shift.html)
157+

0 commit comments

Comments
 (0)