[
math
]
Leetcode 1185. Day of the Week
Problem statement
https://leetcode.com/problems/day-of-the-week/
Solution
Just use bunch of formulas to calculate it.
Complexity
It is O(1)
for time and space and handle leap years.
Code
class Solution:
def dayOfTheWeek(self, d, m, y):
M = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
D = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
start = 4
start += (y - 1971) * 365
for x in range(1971, y):
if x % 4 == 0: start += 1
if y % 4 == 0 and y != 2100 and m > 2: start += 1
for x in range(m - 1):
start += M[x]
start += d
return D[start % 7]