The First Day of the Maximum Recorded Degree in Each City — LeetCode 2314 Python Solution

MediumLeetCode PremiumDatabase
Problem
#2314
Reading time
4 min

Table schema

SQL
Table: Weather +-------------+------+ | Column Name | Type | +-------------+------+ | city_id | int | | day | date | | degree | int | +-------------+------+ (city_id, day) is the primary key (combination of columns with unique values) for this table. Each row in this table contains the degree of the weather of a city on a certain day.

Example

SQL
+-------------+------+
| Column Name | Type |
+-------------+------+
| city_id     | int  |
| day         | date |
| degree      | int  |
+-------------+------+
(city_id, day) is the primary key (combination of columns with unique values) for this table.
Each row in this table contains the degree of the weather of a city on a certain day.
All the degrees are recorded in the year 2022.

Python solution

Python
import duckdb
import pandas as pd

def solution(weather: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Weather", weather)
    return con.execute("""WITH
    T AS (
        SELECT
            *,
            RANK() OVER (
                PARTITION BY city_id
                ORDER BY degree DESC, day
            ) AS rk
        FROM Weather
    )
SELECT city_id, day, degree
FROM T
WHERE rk = 1
ORDER BY 1;""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2314. The First Day of the Maximum Recorded Degree in Each City?
LeetCode 2314. The First Day of the Maximum Recorded Degree in Each City is rated Medium on LeetCode.
What topics does LeetCode 2314. The First Day of the Maximum Recorded Degree in Each City cover?
LeetCode 2314. The First Day of the Maximum Recorded Degree in Each City is tagged Database on LeetCode.
Is LeetCode 2314. The First Day of the Maximum Recorded Degree in Each City a premium problem?
Yes. LeetCode 2314. The First Day of the Maximum Recorded Degree in Each City is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

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.

Get Stealth Interview