| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.MinimumTimeDifference; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MinimumTimeDifferenceBucketSort : MinimumTimeDifferenceBase |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="timePoints"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public override int FindMinDifferenceInternal(IList<string> timePoints) |
| | 7 | 24 | | { |
| | 7 | 25 | | var minutes = new bool[DayMinutes]; |
| | | 26 | | |
| | 68 | 27 | | foreach (var timePoint in timePoints) |
| | 24 | 28 | | { |
| | 24 | 29 | | var totalMinutes = (int)TimeSpan.Parse(timePoint).TotalMinutes; |
| | | 30 | | |
| | 24 | 31 | | if (minutes[totalMinutes]) |
| | 1 | 32 | | { |
| | 1 | 33 | | return 0; |
| | | 34 | | } |
| | | 35 | | |
| | 23 | 36 | | minutes[totalMinutes] = true; |
| | 23 | 37 | | } |
| | | 38 | | |
| | 6 | 39 | | var minDifference = int.MaxValue; |
| | 6 | 40 | | var first = -1; |
| | 6 | 41 | | var last = -1; |
| | 6 | 42 | | var previous = -1; |
| | | 43 | | |
| | 17292 | 44 | | for (var i = 0; i < DayMinutes; i++) |
| | 8640 | 45 | | { |
| | 8640 | 46 | | if (!minutes[i]) |
| | 8619 | 47 | | { |
| | 8619 | 48 | | continue; |
| | | 49 | | } |
| | | 50 | | |
| | 21 | 51 | | if (first == -1) |
| | 6 | 52 | | { |
| | 6 | 53 | | first = i; |
| | 6 | 54 | | } |
| | | 55 | | |
| | 21 | 56 | | if (previous != -1) |
| | 15 | 57 | | { |
| | 15 | 58 | | minDifference = Math.Min(minDifference, i - previous); |
| | 15 | 59 | | } |
| | | 60 | | |
| | 21 | 61 | | previous = i; |
| | 21 | 62 | | last = i; |
| | 21 | 63 | | } |
| | | 64 | | |
| | 6 | 65 | | return Math.Min(minDifference, DayMinutes + first - last); |
| | 7 | 66 | | } |
| | | 67 | | } |