Problem 1185

Abstract / Summary

Given a year day number return the day of the week it falls on using a known date reference.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Python 1185.py

class Solution:
    def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
        return date(year, month, day).strftime("%A")
3 lines | 140 bytes | submitted November 2, 2023 | GitHub   tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 59% (100k+ submissions).

Typical patterns to solve this problem:
  • Math
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The provided implementation defines a class Solution with a single method dayOfTheWeek. The method receives three integer parameters-day, month, and year-and returns the full name of the weekday corresponding to that date. Internally it leverages Python’s standard library datetime module to construct a date object and then formats it using strftime with the %A directive, which yields the weekday name in English.

Key Steps

Import and object creation

The method implicitly relies on the date class from the datetime module. When date(year, month, day) is called, the constructor validates the supplied values and creates an immutable date instance representing the calendar day.

date(year, month, day)

Formatting the result

The strftime method of the date object is invoked with the format string "%A". This directive instructs the formatter to produce the locale-dependent full weekday name (e.g., “Monday”, “Tuesday”).

date(year, month, day).strftime("%A")

Return value

The formatted string is returned directly as the output of dayOfTheWeek.

return date(year, month, day).strftime("%A")

Edge Cases Handled

The datetime.date constructor performs comprehensive validation of the input components. It automatically rejects invalid day-month combinations such as February 30 or April 31, raising a ValueError. Leap year handling is built into the module, so dates like February 29 on a leap year are accepted, while the same date on a non-leap year triggers an exception. The method therefore gracefully handles out-of-range values by delegating to the robust validation logic of the standard library.

Performance Optimizations

The implementation is concise and leverages highly optimized C-level code inside the datetime module. By constructing a date object and immediately calling strftime, the solution avoids any manual calculation of offsets, month lengths, or leap year rules. This direct use of the library eliminates overhead associated with iterative or lookup-based approaches, resulting in minimal Python-level processing and fast execution for each call. The absence of intermediate data structures further reduces memory pressure, allowing the method to operate efficiently even when invoked repeatedly.

written by openai gpt

ELI5 - Simplified Explanation

Imagine you have a calendar book that knows every secret about time. This code is like building a tiny robot helper that looks at that book. You give the robot three puzzle pieces: the day, the month, and the year. The robot picks up those pieces and runs over to the calendar book to find the exact page for that date. Once it finds the page, it reads the name of the day, like Monday or Tuesday, written right there on the paper. It is just like asking a grown-up what day of the week a birthday falls on and getting the answer immediately.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 25 | median = 7 | μ = 7.08 | σ = 2.21

Complexity Analysis

Time: constant
As a rule of thumb, the maximum input size for one second execution time is
  • factorial: up to 10 elements
  • exponential: up to 20 elements
  • polynomial: a few thousand elements
  • loglinear: up to 100,000 elements
  • linear: a few million elements
  • logarithmic: a billion elements
  • constant: practically unlimited
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster C++ Python Java TS
WalkCCC C++ Python Java
leetcode.ca C++ Python Java Go TS
Progiez C++ Python Java
github BigEggStudy C#
black-shadows C++
charles-wangkai Java
DimitrisJim C
everthis JS
fartem Ruby
fsq C++
Garvit244 Python
halfrost Go
JoshCrozier JS
keineahnung2345 C++
lkwq007 Python
lzl124631x C++
muhammadfarhankt C
suxrobGM C#
varunu28 Java
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C++, Go, Java, Python, TypeScript
Progiez C++, Java, Python
BigEggStudy C#
black-shadows C++, C++
charles-wangkai Java
DimitrisJim C
everthis JavaScript
fartem Ruby
fsq C++
Garvit244 Python
halfrost Go
JoshCrozier JavaScript
keineahnung2345 C++
lkwq007 Python
lzl124631x C++
muhammadfarhankt C
suxrobGM C#
varunu28 Java
31 links | 20 authors | 9 languages

Videos

6 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

2 easy 2 medium
5 solutions   November 2, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality