Report Contiguous Dates — LeetCode 1225 Python Solution

HardLeetCode PremiumDatabase
Problem
#1225
Reading time
6 min

Table schema

SQL
Table: Failed +--------------+---------+ | Column Name | Type | +--------------+---------+ | fail_date | date | +--------------+---------+ fail_date is the primary key (column with unique values) for this table. This table contains the days of failed tasks.

Example

SQL
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| fail_date    | date    |
+--------------+---------+
fail_date is the primary key (column with unique values) for this table.
This table contains the days of failed tasks.

Python solution

Python
import duckdb
import pandas as pd

def solution(failed: pd.DataFrame, succeeded: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Failed", failed)
    con.register("Succeeded", succeeded)
    return con.execute("""WITH
    T AS (
        SELECT fail_date AS dt, 'failed' AS st
        FROM Failed
        WHERE YEAR(fail_date) = 2019
        UNION ALL
        SELECT success_date AS dt, 'succeeded' AS st
        FROM Succeeded
        WHERE YEAR(success_date) = 2019
    )
SELECT
    st AS period_state,
    MIN(dt) AS start_date,
    MAX(dt) AS end_date
FROM
    (
        SELECT
            *,
            SUBDATE(
                dt,
                RANK() OVER (
                    PARTITION BY st
                    ORDER BY dt
                )
            ) AS pt
        FROM T
    ) AS t
GROUP BY 1, pt
ORDER BY 2;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 1225. Report Contiguous Dates?
LeetCode 1225. Report Contiguous Dates is rated Hard on LeetCode.
What topics does LeetCode 1225. Report Contiguous Dates cover?
LeetCode 1225. Report Contiguous Dates is tagged Database on LeetCode.
Is LeetCode 1225. Report Contiguous Dates a premium problem?
Yes. LeetCode 1225. Report Contiguous Dates 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