| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.DayOfTheWeek; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DayOfTheWeekMath : IDayOfTheWeek |
| | 16 | | { |
| 12 | 17 | | private readonly Dictionary<int, string> _weekDays = new() |
| 12 | 18 | | { |
| 12 | 19 | | { 0, "Monday" }, |
| 12 | 20 | | { 1, "Tuesday" }, |
| 12 | 21 | | { 2, "Wednesday" }, |
| 12 | 22 | | { 3, "Thursday" }, |
| 12 | 23 | | { 4, "Friday" }, |
| 12 | 24 | | { 5, "Saturday" }, |
| 12 | 25 | | { 6, "Sunday" } |
| 12 | 26 | | }; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Time complexity - O(1) |
| | 30 | | /// Space complexity - O(1) |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="day"></param> |
| | 33 | | /// <param name="month"></param> |
| | 34 | | /// <param name="year"></param> |
| | 35 | | /// <returns></returns> |
| | 36 | | public string DayOfTheWeek(int day, int month, int year) |
| 12 | 37 | | { |
| 12 | 38 | | if (month < 3) |
| 5 | 39 | | { |
| 5 | 40 | | month += 12; |
| 5 | 41 | | year -= 1; |
| 5 | 42 | | } |
| | 43 | |
|
| 12 | 44 | | var k = year % 100; |
| 12 | 45 | | var j = year / 100; |
| | 46 | |
|
| 12 | 47 | | var h = (day + (13 * (month + 1) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7; |
| | 48 | |
|
| 12 | 49 | | var dayOfWeek = (h + 5) % 7; |
| | 50 | |
|
| 12 | 51 | | return _weekDays[dayOfWeek]; |
| 12 | 52 | | } |
| | 53 | | } |