Last updated on October 10th, 2024 at 12:27 am
This Leetcode problem Rising Temperature LeetCode Solution is done in SQL.
List of all LeetCode Solution
Level of Question
Easy
Rising Temperature LeetCode Solution
Table of Contents
Problem Statement
Column Name | Type |
id | int |
recordDate | date |
temperature | int |
Weather
id is the column with unique values for this table. There are no different rows with the same recordDate. This table contains information about the temperature on a certain day.
Write a solution to find all dates’ Id
with higher temperatures compared to its previous dates (yesterday).
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
id | recordDate | temperature |
1 | 2015-01-01 | 10 |
2 | 2015-01-02 | 25 |
3 | 2015-01-03 | 20 |
4 | 2015-01-04 | 30 |
Output:
id |
2 |
4 |
Explanation: In 2015-01-02, the temperature was higher than the previous day (10 -> 25). In 2015-01-04, the temperature was higher than the previous day (20 -> 30).
1. Rising Temperature LeetCode Solution MySQL
select w1.Id from Weather as w1, Weather as w2 where datediff(w1.RecordDate, w2.RecordDate) = 1 and w1.Temperature > w2.Temperature;
2. Rising Temperature LeetCode Solution Pandas
import pandas as pd def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame: weather['recordDate'] = pd.to_datetime(weather['recordDate']) weather_shifted = weather.copy() weather_shifted['recordDate'] = weather_shifted['recordDate'] + pd.to_timedelta(1, unit='D') merged_df = pd.merge(weather, weather_shifted, on='recordDate', suffixes=('_today', '_yesterday')) result = merged_df[merged_df['temperature_today'] > merged_df['temperature_yesterday']][['id_today']].rename(columns={'id_today': 'Id'}) return result