| | 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.AllOneDataStructure; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class AllOneDataStructureLinkedList : IAllOneDataStructure |
| | 16 | | { |
| 1 | 17 | | private readonly Node _headNode = new(int.MinValue); |
| 1 | 18 | | private readonly Dictionary<string, Node> _keyNodeDictionary = new(); |
| 1 | 19 | | private readonly Node _tailNode = new(int.MaxValue); |
| | 20 | |
|
| 1 | 21 | | public AllOneDataStructureLinkedList() |
| 1 | 22 | | { |
| 1 | 23 | | _headNode.NextNode = _tailNode; |
| 1 | 24 | | _tailNode.PreviousNode = _headNode; |
| 1 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Time complexity - O(1) |
| | 29 | | /// Space complexity - O(n) |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="key"></param> |
| | 32 | | public void Inc(string key) |
| 3 | 33 | | { |
| 3 | 34 | | if (_keyNodeDictionary.TryGetValue(key, out var currentNode)) |
| 1 | 35 | | { |
| 1 | 36 | | var nextNode = currentNode.NextNode; |
| | 37 | |
|
| 1 | 38 | | if (nextNode == null) |
| 0 | 39 | | { |
| 0 | 40 | | return; |
| | 41 | | } |
| | 42 | |
|
| 1 | 43 | | if (nextNode.Count != currentNode.Count + 1) |
| 1 | 44 | | { |
| 1 | 45 | | var newNode = new Node(currentNode.Count + 1); |
| | 46 | |
|
| 1 | 47 | | InsertNodeAfter(currentNode, newNode); |
| | 48 | |
|
| 1 | 49 | | nextNode = newNode; |
| 1 | 50 | | } |
| | 51 | |
|
| 1 | 52 | | MoveKey(key, currentNode, nextNode); |
| 1 | 53 | | } |
| | 54 | | else |
| 2 | 55 | | { |
| 2 | 56 | | if (_headNode.NextNode == null) |
| 0 | 57 | | { |
| 0 | 58 | | return; |
| | 59 | | } |
| | 60 | |
|
| 2 | 61 | | if (_headNode.NextNode.Count != 1) |
| 2 | 62 | | { |
| 2 | 63 | | var newNode = new Node(1); |
| | 64 | |
|
| 2 | 65 | | InsertNodeAfter(_headNode, newNode); |
| 2 | 66 | | } |
| | 67 | |
|
| 2 | 68 | | _headNode.NextNode.KeysHashSet.Add(key); |
| 2 | 69 | | _keyNodeDictionary[key] = _headNode.NextNode; |
| 2 | 70 | | } |
| 3 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Time complexity - O(1) |
| | 75 | | /// Space complexity - O(n) |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="key"></param> |
| | 78 | | public void Dec(string key) |
| 0 | 79 | | { |
| 0 | 80 | | if (!_keyNodeDictionary.TryGetValue(key, out var currentNode)) |
| 0 | 81 | | { |
| 0 | 82 | | return; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | if (currentNode.Count == 1) |
| 0 | 86 | | { |
| 0 | 87 | | _keyNodeDictionary.Remove(key); |
| | 88 | |
|
| 0 | 89 | | currentNode.KeysHashSet.Remove(key); |
| | 90 | |
|
| 0 | 91 | | if (currentNode.KeysHashSet.Count == 0) |
| 0 | 92 | | { |
| 0 | 93 | | RemoveNode(currentNode); |
| 0 | 94 | | } |
| 0 | 95 | | } |
| | 96 | | else |
| 0 | 97 | | { |
| 0 | 98 | | var prevNode = currentNode.PreviousNode; |
| | 99 | |
|
| 0 | 100 | | if (prevNode == null) |
| 0 | 101 | | { |
| 0 | 102 | | return; |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | if (prevNode.Count != currentNode.Count - 1) |
| 0 | 106 | | { |
| 0 | 107 | | var newNode = new Node(currentNode.Count - 1); |
| | 108 | |
|
| 0 | 109 | | InsertNodeAfter(prevNode, newNode); |
| | 110 | |
|
| 0 | 111 | | prevNode = newNode; |
| 0 | 112 | | } |
| | 113 | |
|
| 0 | 114 | | MoveKey(key, currentNode, prevNode); |
| 0 | 115 | | } |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Time complexity - O(1) |
| | 120 | | /// Space complexity - O(n) |
| | 121 | | /// </summary> |
| | 122 | | /// <returns></returns> |
| | 123 | | public string GetMaxKey() |
| 2 | 124 | | { |
| 2 | 125 | | if (_tailNode.PreviousNode == null) |
| 0 | 126 | | { |
| 0 | 127 | | return string.Empty; |
| | 128 | | } |
| | 129 | |
|
| 2 | 130 | | return _tailNode.PreviousNode == _headNode ? string.Empty : GetFirstKey(_tailNode.PreviousNode); |
| 2 | 131 | | } |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// Time complexity - O(1) |
| | 135 | | /// Space complexity - O(n) |
| | 136 | | /// </summary> |
| | 137 | | /// <returns></returns> |
| | 138 | | public string GetMinKey() |
| 2 | 139 | | { |
| 2 | 140 | | if (_headNode.NextNode == null) |
| 0 | 141 | | { |
| 0 | 142 | | return string.Empty; |
| | 143 | | } |
| | 144 | |
|
| 2 | 145 | | return _headNode.NextNode == _tailNode ? string.Empty : GetFirstKey(_headNode.NextNode); |
| 2 | 146 | | } |
| | 147 | |
|
| | 148 | | private void MoveKey(string key, Node fromNode, Node toNode) |
| 1 | 149 | | { |
| 1 | 150 | | fromNode.KeysHashSet.Remove(key); |
| | 151 | |
|
| 1 | 152 | | if (fromNode.KeysHashSet.Count == 0) |
| 1 | 153 | | { |
| 1 | 154 | | RemoveNode(fromNode); |
| 1 | 155 | | } |
| | 156 | |
|
| 1 | 157 | | toNode.KeysHashSet.Add(key); |
| | 158 | |
|
| 1 | 159 | | _keyNodeDictionary[key] = toNode; |
| 1 | 160 | | } |
| | 161 | |
|
| | 162 | | private static void InsertNodeAfter(Node prevNode, Node newNode) |
| 3 | 163 | | { |
| 3 | 164 | | newNode.PreviousNode = prevNode; |
| 3 | 165 | | newNode.NextNode = prevNode.NextNode; |
| | 166 | |
|
| 3 | 167 | | if (prevNode.NextNode != null) |
| 3 | 168 | | { |
| 3 | 169 | | prevNode.NextNode.PreviousNode = newNode; |
| 3 | 170 | | } |
| | 171 | |
|
| 3 | 172 | | prevNode.NextNode = newNode; |
| 3 | 173 | | } |
| | 174 | |
|
| | 175 | | private static void RemoveNode(Node node) |
| 1 | 176 | | { |
| 1 | 177 | | if (node.PreviousNode != null) |
| 1 | 178 | | { |
| 1 | 179 | | node.PreviousNode.NextNode = node.NextNode; |
| 1 | 180 | | } |
| | 181 | |
|
| 1 | 182 | | if (node.NextNode != null) |
| 1 | 183 | | { |
| 1 | 184 | | node.NextNode.PreviousNode = node.PreviousNode; |
| 1 | 185 | | } |
| 1 | 186 | | } |
| | 187 | |
|
| | 188 | | private static string GetFirstKey(Node node) |
| 4 | 189 | | { |
| 4 | 190 | | return node.KeysHashSet.Count > 0 ? node.KeysHashSet.First() : string.Empty; |
| 4 | 191 | | } |
| | 192 | |
|
| 5 | 193 | | private class Node(int count) |
| | 194 | | { |
| 10 | 195 | | public int Count { get; } = count; |
| | 196 | |
|
| 18 | 197 | | public HashSet<string> KeysHashSet { get; } = []; |
| | 198 | |
|
| 17 | 199 | | public Node? PreviousNode { get; set; } |
| | 200 | |
|
| 35 | 201 | | public Node? NextNode { get; set; } |
| | 202 | | } |
| | 203 | | } |