HardNo. 1225LeetCode Premium

Report Contiguous Dates — LeetCode 1225 Python Solution

Topics
Database
Reading time
6 min

Table schema

SQLTable schema
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

SQLExample
+--------------+---------+
| 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

PythonSolution
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

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

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

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.

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