| | | 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.ImplementRouter; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ImplementRouterDictionaryWithBinarySearch : IImplementRouter |
| | | 16 | | { |
| | 1 | 17 | | private static readonly int[] ForwardBuffer = new int[3]; |
| | | 18 | | private readonly Dictionary<int, TimestampBuffer> _destinationToTimestampBufferDictionary; |
| | | 19 | | private readonly int _memoryLimit; |
| | | 20 | | private readonly HashSet<(int Source, int Destination, int Timestamp)> _packetsHashSet; |
| | | 21 | | private readonly Queue<(int Source, int Destination, int Timestamp)> _packetsQueue; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Time complexity - O(1) |
| | | 25 | | /// Space complexity - O(n) |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="memoryLimit"></param> |
| | 2 | 28 | | public ImplementRouterDictionaryWithBinarySearch(int memoryLimit) |
| | 2 | 29 | | { |
| | 2 | 30 | | _memoryLimit = memoryLimit; |
| | 2 | 31 | | _packetsHashSet = new HashSet<(int Source, int Destination, int Timestamp)>(memoryLimit); |
| | 2 | 32 | | _packetsQueue = new Queue<(int Source, int Destination, int Timestamp)>(memoryLimit); |
| | 2 | 33 | | _destinationToTimestampBufferDictionary = new Dictionary<int, TimestampBuffer>(memoryLimit); |
| | 2 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Time complexity - O(1) |
| | | 38 | | /// Space complexity - O(1) |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="source"></param> |
| | | 41 | | /// <param name="destination"></param> |
| | | 42 | | /// <param name="timestamp"></param> |
| | | 43 | | /// <returns></returns> |
| | | 44 | | public bool AddPacket(int source, int destination, int timestamp) |
| | 8 | 45 | | { |
| | 8 | 46 | | var packet = (source, destination, timestamp); |
| | | 47 | | |
| | 8 | 48 | | if (!_packetsHashSet.Add(packet)) |
| | 1 | 49 | | { |
| | 1 | 50 | | return false; |
| | | 51 | | } |
| | | 52 | | |
| | 7 | 53 | | if (_packetsQueue.Count == _memoryLimit) |
| | 1 | 54 | | { |
| | 1 | 55 | | ForwardPacket(); |
| | 1 | 56 | | } |
| | | 57 | | |
| | 7 | 58 | | _packetsQueue.Enqueue(packet); |
| | | 59 | | |
| | 7 | 60 | | if (!_destinationToTimestampBufferDictionary.TryGetValue(destination, out var timestampBuffer)) |
| | 4 | 61 | | { |
| | 4 | 62 | | timestampBuffer = new TimestampBuffer(); |
| | | 63 | | |
| | 4 | 64 | | _destinationToTimestampBufferDictionary[destination] = timestampBuffer; |
| | 4 | 65 | | } |
| | | 66 | | |
| | 7 | 67 | | timestampBuffer.Add(timestamp); |
| | | 68 | | |
| | 7 | 69 | | return true; |
| | 8 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Time complexity - O(1) |
| | | 74 | | /// Space complexity - O(1) |
| | | 75 | | /// </summary> |
| | | 76 | | /// <returns></returns> |
| | | 77 | | public int[] ForwardPacket() |
| | 2 | 78 | | { |
| | 2 | 79 | | if (_packetsQueue.Count == 0) |
| | 0 | 80 | | { |
| | 0 | 81 | | return []; |
| | | 82 | | } |
| | | 83 | | |
| | 2 | 84 | | var packet = _packetsQueue.Dequeue(); |
| | | 85 | | |
| | 2 | 86 | | _packetsHashSet.Remove(packet); |
| | | 87 | | |
| | 2 | 88 | | _destinationToTimestampBufferDictionary[packet.Destination].RemoveHead(); |
| | | 89 | | |
| | 2 | 90 | | return GetForwardBuffer(packet.Source, packet.Destination, packet.Timestamp); |
| | 2 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Time complexity - O(log m), where m is the number of timestamps for this destination |
| | | 95 | | /// Space complexity - O(1) |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="destination"></param> |
| | | 98 | | /// <param name="startTime"></param> |
| | | 99 | | /// <param name="endTime"></param> |
| | | 100 | | /// <returns></returns> |
| | | 101 | | public int GetCount(int destination, int startTime, int endTime) |
| | 2 | 102 | | { |
| | 2 | 103 | | return _destinationToTimestampBufferDictionary.TryGetValue(destination, out var packetBuffer) |
| | 2 | 104 | | ? packetBuffer.GetCountInRange(startTime, endTime) |
| | 2 | 105 | | : 0; |
| | 2 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Time complexity - O(1) |
| | | 110 | | /// Space complexity - O(1) |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="source"></param> |
| | | 113 | | /// <param name="destination"></param> |
| | | 114 | | /// <param name="timestamp"></param> |
| | | 115 | | /// <returns></returns> |
| | | 116 | | private static int[] GetForwardBuffer(int source, int destination, int timestamp) |
| | 2 | 117 | | { |
| | 2 | 118 | | ForwardBuffer[0] = source; |
| | 2 | 119 | | ForwardBuffer[1] = destination; |
| | 2 | 120 | | ForwardBuffer[2] = timestamp; |
| | | 121 | | |
| | 2 | 122 | | return ForwardBuffer; |
| | 2 | 123 | | } |
| | | 124 | | |
| | | 125 | | private class TimestampBuffer |
| | | 126 | | { |
| | 4 | 127 | | private readonly List<int> _timestamps = []; |
| | | 128 | | private int _head; |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Time complexity - O(1), O(n) in worst-case on resize |
| | | 132 | | /// Space complexity - O(1) |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="packet"></param> |
| | | 135 | | public void Add(int packet) |
| | 7 | 136 | | { |
| | 7 | 137 | | _timestamps.Add(packet); |
| | 7 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Time complexity - O(1) |
| | | 142 | | /// Space complexity - O(1) |
| | | 143 | | /// </summary> |
| | | 144 | | public void RemoveHead() |
| | 2 | 145 | | { |
| | 2 | 146 | | _head++; |
| | 2 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Time complexity - O(log m), where m is the number of timestamps currently stored |
| | | 151 | | /// Space complexity - O(1) |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="startTime"></param> |
| | | 154 | | /// <param name="endTime"></param> |
| | | 155 | | /// <returns></returns> |
| | | 156 | | public int GetCountInRange(int startTime, int endTime) |
| | 2 | 157 | | { |
| | 2 | 158 | | if (_timestamps.Count - _head == 0) |
| | 0 | 159 | | { |
| | 0 | 160 | | return 0; |
| | | 161 | | } |
| | | 162 | | |
| | 2 | 163 | | var packetsCount = _timestamps.Count; |
| | | 164 | | |
| | 2 | 165 | | var left = FindLowerBound(startTime); |
| | 2 | 166 | | var right = FindLowerBound(endTime + 1); |
| | | 167 | | |
| | 2 | 168 | | if (left > right || left >= packetsCount) |
| | 0 | 169 | | { |
| | 0 | 170 | | return 0; |
| | | 171 | | } |
| | | 172 | | |
| | 2 | 173 | | return right - left; |
| | 2 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Time complexity - O(log m), where m is the number of timestamps currently stored |
| | | 178 | | /// Space complexity - O(1) |
| | | 179 | | /// </summary> |
| | | 180 | | /// <param name="target"></param> |
| | | 181 | | /// <returns></returns> |
| | | 182 | | private int FindLowerBound(int target) |
| | 4 | 183 | | { |
| | 4 | 184 | | var low = _head; |
| | 4 | 185 | | var high = _timestamps.Count; |
| | | 186 | | |
| | 10 | 187 | | while (low < high) |
| | 6 | 188 | | { |
| | 6 | 189 | | var mid = low + ((high - low) / 2); |
| | | 190 | | |
| | 6 | 191 | | if (_timestamps[mid] < target) |
| | 3 | 192 | | { |
| | 3 | 193 | | low = mid + 1; |
| | 3 | 194 | | } |
| | | 195 | | else |
| | 3 | 196 | | { |
| | 3 | 197 | | high = mid; |
| | 3 | 198 | | } |
| | 6 | 199 | | } |
| | | 200 | | |
| | 4 | 201 | | return high; |
| | 4 | 202 | | } |
| | | 203 | | } |
| | | 204 | | } |