Advertisement

Number of Days Between Two Dates - LeetCode 1360 Solution

Number of Days Between Two Dates - Complete Solution Guide

Number of Days Between Two Dates is LeetCode problem 1360, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Write a program to count the number of days between two dates. The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples. Example 1: Input: date1 = "2019-06-29", date2 = "2019-06-30" Output: 1 Example 2: Input: date1 = "2020-01-15", date2 = "2019-12-31" Output: 15 Constraints: The given dates are valid dates between the years 1971 and 2100 .

Detailed Explanation

This problem challenges us to compute the absolute difference in days between two given dates, presented as "YYYY-MM-DD" strings. At first glance, it seems like a straightforward date arithmetic task, but the devil's in the details: correctly accounting for variable month lengths and, crucially, the intricacies of leap years. The dates are guaranteed to be valid and fall within the years 1971 and 2100, which is a helpful constraint but doesn't let us off the hook for implementing proper calendrical logic.

Solution Approach

The most robust and elegant approach to finding the difference between two dates is to normalize both dates to a single, comparable integer representing the total number of days passed since a fixed reference point. In the provided solution, this reference point is implicitly January 1st, 1971, which aligns with the problem's earliest date constraint. By converting both `date1` and `date2` into their respective total day counts from 1971 (let's call them `total_days_1` and `total_days_2`), the complex date subtraction problem simplifies into a straightforward `abs(total_days_1 - total_days_2)` operation.

Step-by-Step Algorithm

  1. Step 1: Parse the input date strings into year, month, and day components.
  2. Step 2: Determine if each year encountered is a leap year using the leap year rule.
  3. Step 3: Calculate the number of days from the reference date to the input date. This involves iterating through years, adding the number of days in each year (365 or 366 based on leap year status), and adding the days in the months up to the target month and day.
  4. Step 4: Calculate the absolute difference between the day counts for the two input dates.
  5. Step 5: Return the absolute difference as the final result.

Key Insights

  • **Epoch Normalization for Date Arithmetic:** The core insight is to convert each date string into a single, unambiguous integer representing its total day count from a fixed epoch (e.g., January 1st, 1971). This transforms a potentially messy date-difference calculation involving year/month boundaries and variable month lengths into a simple integer subtraction.
  • **Precise Leap Year and Month Length Rules:** Correctly implementing the `is_leap` function (divisible by 4, unless by 100 but not by 400) and `days_in_month` (handling 28/29 for Feb, 30 for April/June/Sept/Nov, 31 for others) is absolutely fundamental. Any misstep here will propagate errors throughout the day count, especially when dates cross year boundaries or involve February.
  • **Accumulative Summation for Robust Day Counting:** The `days_from_1971` helper function is built on an accumulative summation strategy. It first sums days for all *entire years* before the target date's year, then sums days for all *entire months* before the target date's month within that year, and finally adds the day component itself. This systematic, additive approach is less prone to off-by-one errors compared to trying to directly calculate differences between date components.

Complexity Analysis

Time Complexity: O(Y)

Space Complexity: O(1)

Topics

This problem involves: Math, String.

Companies

Asked at: Accenture, Optiver.