LeetCode 1185: Day of the Week Solution
Master LeetCode problem 1185 (Day of the Week), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1185. Day of the Week
Problem Explanation
Explanation
To solve this problem, we can use Zeller's Congruence algorithm to calculate the day of the week for a given date. The algorithm involves a formula that takes into account the day, month, and year of the date and returns a number corresponding to the day of the week. We can then map this number to the actual day name.
Algorithm
-
Adjust the month and year for Zeller's Congruence formula:
- If the month is January or February, treat them as months 13 and 14 of the previous year.
- Adjust the year accordingly.
-
Apply Zeller's Congruence formula:
- Calculate the day of the week using the formula:
[
h = (q + \left\lfloor \frac{13(m+1)}{5} \right\rfloor + K + \left\lfloor \frac{K}{4} \right\rfloor + \left\lfloor \frac{J}{4} \right\rfloor - 2J) \mod 7
]
where:
- ( h ) is the day of the week (0 for Saturday, 1 for Sunday, ..., 6 for Friday)
- ( q ) is the day of the month
- ( m ) is the adjusted month
- ( K ) is the year of the century (year % 100)
- ( J ) is the zero-based century (year / 100)
- Calculate the day of the week using the formula:
[
h = (q + \left\lfloor \frac{13(m+1)}{5} \right\rfloor + K + \left\lfloor \frac{K}{4} \right\rfloor + \left\lfloor \frac{J}{4} \right\rfloor - 2J) \mod 7
]
where:
-
Map the calculated day of the week value to the corresponding day name.
Time Complexity
The time complexity of this algorithm is O(1) as it involves a fixed number of arithmetic operations.
Space Complexity
The space complexity is O(1) as we are using only a constant amount of extra space.
Solution Code
class Solution {
public String dayOfTheWeek(int day, int month, int year) {
String[] daysOfWeek = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
if (month < 3) {
month += 12;
year--;
}
int q = day;
int m = month;
int K = year % 100;
int J = year / 100;
int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) - 2 * J) % 7;
return daysOfWeek[(h + 5) % 7]; // Adjust for Zeller's Congruence result
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1185 (Day of the Week)?
This page provides optimized solutions for LeetCode problem 1185 (Day of the Week) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1185 (Day of the Week)?
The time complexity for LeetCode 1185 (Day of the Week) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1185 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1185 in Java, C++, or Python.