Rising Temperature LeetCode Solution

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

Rising Temperature LeetCode Solution

Problem Statement

Column NameType
idint
recordDatedate
temperatureint
Table: 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 recordDatetemperature
12015-01-0110
22015-01-0225
32015-01-0320
42015-01-0430
Weather table:

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
Scroll to Top