|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3508\. Implement Router |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes: |
| 9 | + |
| 10 | +* `source`: A unique identifier for the machine that generated the packet. |
| 11 | +* `destination`: A unique identifier for the target machine. |
| 12 | +* `timestamp`: The time at which the packet arrived at the router. |
| 13 | + |
| 14 | +Implement the `Router` class: |
| 15 | + |
| 16 | +`Router(int memoryLimit)`: Initializes the Router object with a fixed memory limit. |
| 17 | + |
| 18 | +* `memoryLimit` is the **maximum** number of packets the router can store at any given time. |
| 19 | +* If adding a new packet would exceed this limit, the **oldest** packet must be removed to free up space. |
| 20 | + |
| 21 | +`bool addPacket(int source, int destination, int timestamp)`: Adds a packet with the given attributes to the router. |
| 22 | + |
| 23 | +* A packet is considered a duplicate if another packet with the same `source`, `destination`, and `timestamp` already exists in the router. |
| 24 | +* Return `true` if the packet is successfully added (i.e., it is not a duplicate); otherwise return `false`. |
| 25 | + |
| 26 | +`int[] forwardPacket()`: Forwards the next packet in FIFO (First In First Out) order. |
| 27 | + |
| 28 | +* Remove the packet from storage. |
| 29 | +* Return the packet as an array `[source, destination, timestamp]`. |
| 30 | +* If there are no packets to forward, return an empty array. |
| 31 | + |
| 32 | +`int getCount(int destination, int startTime, int endTime)`: |
| 33 | + |
| 34 | +* Returns the number of packets currently stored in the router (i.e., not yet forwarded) that have the specified destination and have timestamps in the inclusive range `[startTime, endTime]`. |
| 35 | + |
| 36 | +**Note** that queries for `addPacket` will be made in increasing order of `timestamp`. |
| 37 | + |
| 38 | +**Example 1:** |
| 39 | + |
| 40 | +**Input:** |
| 41 | + ["Router", "addPacket", "addPacket", "addPacket", "addPacket", "addPacket", "forwardPacket", "addPacket", "getCount"] |
| 42 | + [[3], [1, 4, 90], [2, 5, 90], [1, 4, 90], [3, 5, 95], [4, 5, 105], [], [5, 2, 110], [5, 100, 110]] |
| 43 | + |
| 44 | +**Output:** |
| 45 | + [null, true, true, false, true, true, [2, 5, 90], true, 1] |
| 46 | + |
| 47 | +**Explanation** |
| 48 | + |
| 49 | +Router router = new Router(3); // Initialize Router with memoryLimit of 3. |
| 50 | + router.addPacket(1, 4, 90); // Packet is added. Return True. |
| 51 | + router.addPacket(2, 5, 90); // Packet is added. Return True. |
| 52 | + router.addPacket(1, 4, 90); // This is a duplicate packet. Return False. |
| 53 | + router.addPacket(3, 5, 95); // Packet is added. Return True |
| 54 | + router.addPacket(4, 5, 105); // Packet is added, `[1, 4, 90]` is removed as number of packets exceeds memoryLimit. Return True. |
| 55 | + router.forwardPacket(); // Return `[2, 5, 90]` and remove it from router. |
| 56 | + router.addPacket(5, 2, 110); // Packet is added. Return True. |
| 57 | + router.getCount(5, 100, 110); // The only packet with destination 5 and timestamp in the inclusive range `[100, 110]` is `[4, 5, 105]`. Return 1. |
| 58 | + |
| 59 | +**Example 2:** |
| 60 | + |
| 61 | +**Input:** |
| 62 | + ["Router", "addPacket", "forwardPacket", "forwardPacket"] |
| 63 | + [[2], [7, 4, 90], [], []] |
| 64 | + |
| 65 | +**Output:** |
| 66 | + [null, true, [7, 4, 90], []] |
| 67 | + |
| 68 | +**Explanation** |
| 69 | + |
| 70 | +Router router = new Router(2); // Initialize `Router` with `memoryLimit` of 2. |
| 71 | + router.addPacket(7, 4, 90); // Return True. |
| 72 | + router.forwardPacket(); // Return `[7, 4, 90]`. |
| 73 | + router.forwardPacket(); // There are no packets left, return `[]`. |
| 74 | + |
| 75 | +**Constraints:** |
| 76 | + |
| 77 | +* <code>2 <= memoryLimit <= 10<sup>5</sup></code> |
| 78 | +* <code>1 <= source, destination <= 2 * 10<sup>5</sup></code> |
| 79 | +* <code>1 <= timestamp <= 10<sup>9</sup></code> |
| 80 | +* <code>1 <= startTime <= endTime <= 10<sup>9</sup></code> |
| 81 | +* At most <code>10<sup>5</sup></code> calls will be made to `addPacket`, `forwardPacket`, and `getCount` methods altogether. |
| 82 | +* queries for `addPacket` will be made in increasing order of `timestamp`. |
| 83 | + |
| 84 | +## Solution |
| 85 | + |
| 86 | +```kotlin |
| 87 | +import java.util.LinkedList |
| 88 | +import java.util.Queue |
| 89 | +import kotlin.math.max |
| 90 | + |
| 91 | +class Router(private val size: Int) { |
| 92 | + private var cur = 0 |
| 93 | + private val q: Queue<IntArray> |
| 94 | + private val map: HashMap<Int, ArrayList<IntArray>> |
| 95 | + |
| 96 | + init { |
| 97 | + q = LinkedList<IntArray>() |
| 98 | + map = HashMap<Int, ArrayList<IntArray>>() |
| 99 | + } |
| 100 | + |
| 101 | + fun addPacket(source: Int, destination: Int, timestamp: Int): Boolean { |
| 102 | + if (map.containsKey(destination)) { |
| 103 | + var found = false |
| 104 | + val list: ArrayList<IntArray> = map[destination]!! |
| 105 | + for (i in list.indices.reversed()) { |
| 106 | + if (list[i][1] < timestamp) { |
| 107 | + break |
| 108 | + } else if (list[i][0] == source) { |
| 109 | + found = true |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + if (found) { |
| 114 | + return false |
| 115 | + } |
| 116 | + } |
| 117 | + if (map.containsKey(destination)) { |
| 118 | + val list: ArrayList<IntArray> = map[destination]!! |
| 119 | + list.add(intArrayOf(source, timestamp)) |
| 120 | + cur++ |
| 121 | + q.offer(intArrayOf(source, destination, timestamp)) |
| 122 | + } else { |
| 123 | + val temp = ArrayList<IntArray>() |
| 124 | + temp.add(intArrayOf(source, timestamp)) |
| 125 | + cur++ |
| 126 | + map.put(destination, temp) |
| 127 | + q.offer(intArrayOf(source, destination, timestamp)) |
| 128 | + } |
| 129 | + if (cur > size) { |
| 130 | + forwardPacket() |
| 131 | + } |
| 132 | + return true |
| 133 | + } |
| 134 | + |
| 135 | + fun forwardPacket(): IntArray? { |
| 136 | + if (q.isEmpty()) { |
| 137 | + return intArrayOf() |
| 138 | + } |
| 139 | + val temp = q.poll() |
| 140 | + val list: ArrayList<IntArray> = map[temp[1]]!! |
| 141 | + list.removeAt(0) |
| 142 | + if (list.isEmpty()) { |
| 143 | + map.remove(temp[1]) |
| 144 | + } |
| 145 | + cur-- |
| 146 | + return temp |
| 147 | + } |
| 148 | + |
| 149 | + fun getCount(destination: Int, startTime: Int, endTime: Int): Int { |
| 150 | + return if (map.containsKey(destination)) { |
| 151 | + val list: ArrayList<IntArray> = map[destination]!! |
| 152 | + var lower = -1 |
| 153 | + var higher = -1 |
| 154 | + for (i in list.indices) { |
| 155 | + if (list[i][1] >= startTime) { |
| 156 | + lower = i |
| 157 | + break |
| 158 | + } |
| 159 | + } |
| 160 | + for (i in list.indices.reversed()) { |
| 161 | + if (list[i][1] <= endTime) { |
| 162 | + higher = i |
| 163 | + break |
| 164 | + } |
| 165 | + } |
| 166 | + if (lower == -1 || higher == -1) { |
| 167 | + 0 |
| 168 | + } else { |
| 169 | + max(0.0, (higher - lower + 1).toDouble()).toInt() |
| 170 | + } |
| 171 | + } else { |
| 172 | + 0 |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/* |
| 178 | + * Your Router object will be instantiated and called as such: |
| 179 | + * var obj = Router(memoryLimit) |
| 180 | + * var param_1 = obj.addPacket(source,destination,timestamp) |
| 181 | + * var param_2 = obj.forwardPacket() |
| 182 | + * var param_3 = obj.getCount(destination,startTime,endTime) |
| 183 | + */ |
| 184 | +``` |
0 commit comments