|
| 1 | +[](https://github.com/javadev/LeetCode-in-Java) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Java/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 | +```java |
| 87 | +import java.util.ArrayList; |
| 88 | +import java.util.HashMap; |
| 89 | +import java.util.LinkedList; |
| 90 | +import java.util.Queue; |
| 91 | + |
| 92 | +@SuppressWarnings("java:S135") |
| 93 | +public class Router { |
| 94 | + private final int size; |
| 95 | + private int cur; |
| 96 | + private final Queue<int[]> q; |
| 97 | + private final HashMap<Integer, ArrayList<int[]>> map; |
| 98 | + |
| 99 | + public Router(int memoryLimit) { |
| 100 | + q = new LinkedList<>(); |
| 101 | + map = new HashMap<>(); |
| 102 | + size = memoryLimit; |
| 103 | + cur = 0; |
| 104 | + } |
| 105 | + |
| 106 | + public boolean addPacket(int source, int destination, int timestamp) { |
| 107 | + if (map.containsKey(destination)) { |
| 108 | + boolean found = false; |
| 109 | + ArrayList<int[]> list = map.get(destination); |
| 110 | + for (int i = list.size() - 1; i >= 0; i--) { |
| 111 | + if (list.get(i)[1] < timestamp) { |
| 112 | + break; |
| 113 | + } else if (list.get(i)[0] == source) { |
| 114 | + found = true; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + if (found) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + } |
| 122 | + if (map.containsKey(destination)) { |
| 123 | + ArrayList<int[]> list = map.get(destination); |
| 124 | + list.add(new int[] {source, timestamp}); |
| 125 | + cur++; |
| 126 | + q.offer(new int[] {source, destination, timestamp}); |
| 127 | + } else { |
| 128 | + ArrayList<int[]> temp = new ArrayList<>(); |
| 129 | + temp.add(new int[] {source, timestamp}); |
| 130 | + cur++; |
| 131 | + map.put(destination, temp); |
| 132 | + q.offer(new int[] {source, destination, timestamp}); |
| 133 | + } |
| 134 | + if (cur > size) { |
| 135 | + forwardPacket(); |
| 136 | + } |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + public int[] forwardPacket() { |
| 141 | + if (q.isEmpty()) { |
| 142 | + return new int[] {}; |
| 143 | + } |
| 144 | + int[] temp = q.poll(); |
| 145 | + ArrayList<int[]> list = map.get(temp[1]); |
| 146 | + list.remove(0); |
| 147 | + if (list.isEmpty()) { |
| 148 | + map.remove(temp[1]); |
| 149 | + } |
| 150 | + cur--; |
| 151 | + return temp; |
| 152 | + } |
| 153 | + |
| 154 | + public int getCount(int destination, int startTime, int endTime) { |
| 155 | + if (map.containsKey(destination)) { |
| 156 | + ArrayList<int[]> list = map.get(destination); |
| 157 | + int lower = -1; |
| 158 | + int higher = -1; |
| 159 | + for (int i = 0; i < list.size(); i++) { |
| 160 | + if (list.get(i)[1] >= startTime) { |
| 161 | + lower = i; |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + for (int i = list.size() - 1; i >= 0; i--) { |
| 166 | + if (list.get(i)[1] <= endTime) { |
| 167 | + higher = i; |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + if (lower == -1 || higher == -1) { |
| 172 | + return 0; |
| 173 | + } else { |
| 174 | + return Math.max(0, higher - lower + 1); |
| 175 | + } |
| 176 | + } else { |
| 177 | + return 0; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +/* |
| 183 | + * Your Router object will be instantiated and called as such: |
| 184 | + * Router obj = new Router(memoryLimit); |
| 185 | + * boolean param_1 = obj.addPacket(source,destination,timestamp); |
| 186 | + * int[] param_2 = obj.forwardPacket(); |
| 187 | + * int param_3 = obj.getCount(destination,startTime,endTime); |
| 188 | + */ |
| 189 | +``` |
0 commit comments