EasyNo. 0197

Rising Temperature — LeetCode 197 Python Solution

Topics
Database
Reading time
2 min

The problem

Find the ids of the days that were warmer than the immediately preceding calendar day. The Weather table has id (int, unique), recordDate (date, never repeated) and temperature (int). Dates can be missing from the table, so a day only qualifies when the day before it is actually recorded. The result is a single column of ids and may be returned in any order.

Example

Input
Weather(id, recordDate, temperature) = (1, '2024-03-01', 12), (2, '2024-03-02', 18), (3, '2024-03-03', 15), (4, '2024-03-05', 22), (5, '2024-03-06', 25)
Output
id = 2, 5
Explanation
Id 4 is warmer than the previous row but 2024-03-04 is not in the table, so only ids 2 and 5 follow a recorded, cooler day.

Python solution

PythonSolution
import pandas as pd


def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
    weather.sort_values(by="recordDate", inplace=True)
    return weather[
        (weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1)
    ][["id"]]

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0196Delete Duplicate EmailsEasy
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium

Frequently asked questions

How hard is LeetCode 197. Rising Temperature?

LeetCode 197. Rising Temperature is rated Easy on LeetCode.

What topics does LeetCode 197. Rising Temperature cover?

LeetCode 197. Rising Temperature is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.