| | | 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.InsertDeleteGetRandom; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class InsertDeleteGetRandomDictionaryList : IInsertDeleteGetRandom |
| | | 16 | | { |
| | 1 | 17 | | private static readonly Random Random = new(); |
| | 1 | 18 | | private readonly List<int> _values = []; |
| | 1 | 19 | | private readonly Dictionary<int, int> _valueToIndexDictionary = []; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Time complexity - O(1) |
| | | 23 | | /// Space complexity - O(1) |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="value"></param> |
| | | 26 | | /// <returns></returns> |
| | | 27 | | public bool Insert(int value) |
| | 4 | 28 | | { |
| | 4 | 29 | | if (_valueToIndexDictionary.ContainsKey(value)) |
| | 1 | 30 | | { |
| | 1 | 31 | | return false; |
| | | 32 | | } |
| | | 33 | | |
| | 3 | 34 | | _valueToIndexDictionary.Add(value, _values.Count); |
| | 3 | 35 | | _values.Add(value); |
| | | 36 | | |
| | 3 | 37 | | return true; |
| | 4 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Time complexity - O(1) |
| | | 42 | | /// Space complexity - O(1) |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="value"></param> |
| | | 45 | | /// <returns></returns> |
| | | 46 | | public bool Remove(int value) |
| | 4 | 47 | | { |
| | 4 | 48 | | if (!_valueToIndexDictionary.TryGetValue(value, out var index)) |
| | 2 | 49 | | { |
| | 2 | 50 | | return false; |
| | | 51 | | } |
| | | 52 | | |
| | 2 | 53 | | var lastValueIndex = _values.Count - 1; |
| | 2 | 54 | | var lastValue = _values[lastValueIndex]; |
| | | 55 | | |
| | 2 | 56 | | _values[index] = lastValue; |
| | 2 | 57 | | _values.RemoveAt(lastValueIndex); |
| | | 58 | | |
| | 2 | 59 | | _valueToIndexDictionary[lastValue] = index; |
| | 2 | 60 | | _valueToIndexDictionary.Remove(value); |
| | | 61 | | |
| | 2 | 62 | | return true; |
| | 4 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Time complexity - O(1) |
| | | 67 | | /// Space complexity - O(1) |
| | | 68 | | /// </summary> |
| | | 69 | | /// <returns></returns> |
| | | 70 | | public int GetRandom() |
| | 3 | 71 | | { |
| | 3 | 72 | | var randomIndex = Random.Next(_values.Count); |
| | | 73 | | |
| | 3 | 74 | | return _values[randomIndex]; |
| | 3 | 75 | | } |
| | | 76 | | } |