Domain 1EasyCORE

Day of the Week

The problem requires determining the day of the week for a given date provided as three integers: `day`, `month`, and `year`. The output should be one of the seven days of the week. The input dates are valid between the years 1971 and 2100.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Array

1185. Day of the Week

Model: qwen2.5-coder:14b
Topics: Array
FAANG asked: 0
Acceptance rate: 61.1%


Problem Breakdown

The problem requires determining the day of the week for a given date provided as three integers: day, month, and year. The output should be one of the seven days of the week. The input dates are valid between the years 1971 and 2100.

Inputs:

  • day: An integer representing the day of the month.
  • month: An integer representing the month (1 for January, 12 for December).
  • year: An integer representing the year.

Outputs:

  • A string from the set {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

Implicit Constraints:

  • The input dates are always valid.
  • The range of years (1971 to 2100) is relatively small, which can be leveraged for optimizations.

Intuition

A brute-force approach would involve calculating the number of days from a fixed reference date (e.g., January 1, 1971) up to the given date and then determining the day of the week by taking modulo 7. However, this approach might be inefficient due to repeated calculations for each year.

The key insight is to precompute the cumulative number of days in each month for non-leap years and leap years separately. This allows us to quickly calculate the total number of days from the reference date to any given date by summing up the precomputed values.

Approach

  1. Precompute Days in Each Month:

    • Create two arrays, days_in_month_non_leap and days_in_month_leap, containing the cumulative number of days for each month in non-leap years and leap years, respectively.
  2. Leap Year Check:

    • Implement a helper function to determine if a given year is a leap year.
  3. Calculate Total Days from Reference Date:

    • Calculate the total number of days from January 1, 1971, to the given date by:
      • Adding the cumulative days for each month up to the previous month.
      • Adding the days in the current month.
      • Adding the leap year adjustments.
  4. Determine Day of the Week:

    • Use modulo operation to find the day of the week based on the total number of days from the reference date.

Complexity Analysis

  • Time Complexity: O(1) since all operations are constant-time lookups and arithmetic.
  • Space Complexity: O(1) as we use a fixed amount of extra space for precomputed arrays and variables.

Solution

class Solution:
    def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
        # Precompute cumulative days in each month for non-leap and leap years
        days_in_month_non_leap = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
        days_in_month_leap = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]
        
        # Days of the week starting from Sunday
        days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
        
        # Reference date is January 1, 1971, which was a Friday (index 5)
        reference_day_index = 5
        
        def is_leap_year(y):
            return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
        
        # Calculate total days from January 1, 1971
        total_days = 0
        
        # Add days for full years
        for y in range(1971, year):
            if is_leap_year(y):
                total_days += 366
            else:
                total_days += 365
        
        # Add days for full months in the current year
        if is_leap_year(year):
            total_days += days_in_month_leap[month - 1]
        else:
            total_days += days_in_month_non_leap[month - 1]
        
        # Add remaining days in the current month
        total_days += day
        
        # Calculate the index of the day of the week
        day_index = (reference_day_index + total_days) % 7
        
        return days_of_week[day_index]

Edge Cases Handled

  • Leap Year Calculation: The function correctly identifies leap years using the standard rules.
  • Month and Day Boundaries: The precomputed arrays handle the cumulative days correctly, including month boundaries.
  • Reference Date Consistency: The reference date (January 1, 1971) is consistently used to calculate the day of the week.