| | 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.MyCalendar2; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MyCalendar2BruteForce : IMyCalendar2 |
| | 16 | | { |
| 1 | 17 | | private readonly List<Item> _items = []; |
| 1 | 18 | | private readonly List<Item> _overlapItems = []; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Time complexity - O(n) |
| | 22 | | /// Space complexity - O(n) |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="start"></param> |
| | 25 | | /// <param name="end"></param> |
| | 26 | | /// <returns></returns> |
| | 27 | | public bool Book(int start, int end) |
| 6 | 28 | | { |
| 6 | 29 | | var newItem = new Item(start, end); |
| | 30 | |
|
| 9 | 31 | | if (_overlapItems.Any(overlapItem => start < overlapItem.End && end > overlapItem.Start)) |
| 1 | 32 | | { |
| 1 | 33 | | return false; |
| | 34 | | } |
| | 35 | |
|
| 31 | 36 | | foreach (var item in _items.Where(item => start < item.End && end > item.Start)) |
| 3 | 37 | | { |
| 3 | 38 | | _overlapItems.Add(new Item(Math.Max(start, item.Start), Math.Min(end, item.End))); |
| 3 | 39 | | } |
| | 40 | |
|
| 5 | 41 | | _items.Add(newItem); |
| | 42 | |
|
| 5 | 43 | | return true; |
| 6 | 44 | | } |
| | 45 | |
|
| 9 | 46 | | private class Item(int start, int end) |
| | 47 | | { |
| 21 | 48 | | public int Start { get; } = start; |
| 25 | 49 | | public int End { get; } = end; |
| | 50 | | } |
| | 51 | | } |