| | 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.DesignNumberContainerSystem; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DesignNumberContainerSystemDictionary : IDesignNumberContainerSystem |
| | 16 | | { |
| 1 | 17 | | private readonly Dictionary<int, int> _indexToNumber = []; |
| 1 | 18 | | private readonly Dictionary<int, SortedSet<int>> _numberToIndices = []; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Time complexity - O(log n) |
| | 22 | | /// Space complexity - O(n) |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="index"></param> |
| | 25 | | /// <param name="number"></param> |
| | 26 | | public void Change(int index, int number) |
| 5 | 27 | | { |
| 5 | 28 | | if (_indexToNumber.TryGetValue(index, out var existingNumber)) |
| 1 | 29 | | { |
| 1 | 30 | | if (existingNumber == number) |
| 0 | 31 | | { |
| 0 | 32 | | return; |
| | 33 | | } |
| | 34 | |
|
| 1 | 35 | | if (_numberToIndices.TryGetValue(existingNumber, out var existingNumberIndices)) |
| 1 | 36 | | { |
| 1 | 37 | | existingNumberIndices.Remove(index); |
| | 38 | |
|
| 1 | 39 | | if (existingNumberIndices.Count == 0) |
| 0 | 40 | | { |
| 0 | 41 | | _numberToIndices.Remove(existingNumber); |
| 0 | 42 | | } |
| 1 | 43 | | } |
| 1 | 44 | | } |
| | 45 | |
|
| 5 | 46 | | _indexToNumber[index] = number; |
| | 47 | |
|
| 5 | 48 | | if (!_numberToIndices.TryGetValue(number, out var numberIndices)) |
| 2 | 49 | | { |
| 2 | 50 | | numberIndices = []; |
| | 51 | |
|
| 2 | 52 | | _numberToIndices[number] = numberIndices; |
| 2 | 53 | | } |
| | 54 | |
|
| 5 | 55 | | numberIndices.Add(index); |
| 5 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Time complexity - O(1) |
| | 60 | | /// Space complexity - O(n) |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="number"></param> |
| | 63 | | /// <returns></returns> |
| | 64 | | public int Find(int number) |
| 3 | 65 | | { |
| 3 | 66 | | if (_numberToIndices.TryGetValue(number, out var numberIndices) && numberIndices.Count > 0) |
| 2 | 67 | | { |
| 2 | 68 | | return numberIndices.Min; |
| | 69 | | } |
| | 70 | |
|
| 1 | 71 | | return -1; |
| 3 | 72 | | } |
| | 73 | | } |