| | 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.CountDaysWithoutMeetings; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CountDaysWithoutMeetingsSorting : ICountDaysWithoutMeetings |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n log n) |
| | 19 | | /// Space complexity - O(log n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="days"></param> |
| | 22 | | /// <param name="meetings"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int CountDays(int days, int[][] meetings) |
| 3 | 25 | | { |
| 7 | 26 | | Array.Sort(meetings, (a, b) => a[0].CompareTo(b[0])); |
| | 27 | |
|
| 3 | 28 | | var result = 0; |
| | 29 | |
|
| 3 | 30 | | var previous = 1; |
| | 31 | |
|
| 21 | 32 | | foreach (var meeting in meetings) |
| 6 | 33 | | { |
| 6 | 34 | | var start = meeting[0]; |
| 6 | 35 | | var end = meeting[1]; |
| | 36 | |
|
| 6 | 37 | | if (start > previous) |
| 2 | 38 | | { |
| 2 | 39 | | result += start - previous; |
| 2 | 40 | | } |
| | 41 | |
|
| 6 | 42 | | previous = Math.Max(previous, end + 1); |
| 6 | 43 | | } |
| | 44 | |
|
| 3 | 45 | | if (previous <= days) |
| 1 | 46 | | { |
| 1 | 47 | | result += days - previous + 1; |
| 1 | 48 | | } |
| | 49 | |
|
| 3 | 50 | | return result; |
| 3 | 51 | | } |
| | 52 | | } |