Leetcode Problem 1225. Report Contiguous Dates

1225. Report Contiguous Dates

Leetcode Solutions

Using Window Functions to Identify Continuous Date Intervals

  1. Combine the 'Failed' and 'Succeeded' tables into a single table with an additional column to indicate the state ('failed' or 'succeeded').
  2. Use the ROW_NUMBER() window function to assign a sequential integer to each date, ordered by the date.
  3. Use the DENSE_RANK() window function to assign a rank to each date, ordered by the date, within each state.
  4. Calculate the difference between the row number and the rank for each date to create a unique group identifier for continuous date intervals.
  5. Group by the state and the calculated difference to find the minimum and maximum dates within each group, which represent the start and end dates of each interval.
  6. Order the final result by the start date.
erDiagram
    FAILED ||--|| FAILED : fail_date
    SUCCEEDED ||--|| SUCCEEDED : success_date
    FAILED {
        date fail_date PK
    }
    SUCCEEDED {
        date success_date PK
    }

Using Self-Join to Identify Continuous Date Intervals

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...