| | 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.DayOfTheYear; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DayOfTheYearMath : IDayOfTheYear |
| | 16 | | { |
| 1 | 17 | | private static readonly int[] CumulativeDays = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(1) |
| | 21 | | /// Space complexity - O(1) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="date"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public int DayOfYear(string date) |
| 3 | 26 | | { |
| 3 | 27 | | var dateSpan = date.AsSpan(); |
| | 28 | |
|
| 3 | 29 | | var year = int.Parse(dateSpan[..4]); |
| 3 | 30 | | var month = int.Parse(dateSpan.Slice(5, 2)); |
| 3 | 31 | | var day = int.Parse(dateSpan.Slice(8, 2)); |
| | 32 | |
|
| 3 | 33 | | var dayOfYear = CumulativeDays[month - 1] + day; |
| | 34 | |
|
| 3 | 35 | | if (month > 2 && DateTime.IsLeapYear(year)) |
| 1 | 36 | | { |
| 1 | 37 | | dayOfYear++; |
| 1 | 38 | | } |
| | 39 | |
|
| 3 | 40 | | return dayOfYear; |
| 3 | 41 | | } |
| | 42 | | } |