|
1 | 1 | # [3508.Implement Router][title] |
2 | 2 |
|
3 | | -> [!WARNING|style:flat] |
4 | | -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) |
5 | | -
|
6 | 3 | ## Description |
| 4 | +Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes: |
| 5 | + |
| 6 | +- `source`: A unique identifier for the machine that generated the packet. |
| 7 | +- `destination`: A unique identifier for the target machine. |
| 8 | +- `timestamp`: The time at which the packet arrived at the router. |
| 9 | + |
| 10 | +Implement the `Router` class: |
| 11 | + |
| 12 | +`Router(int memoryLimit)`: Initializes the Router object with a fixed memory limit. |
| 13 | + |
| 14 | +- `memoryLimit` is the **maximum** number of packets the router can store at any given time. |
| 15 | +- If adding a new packet would exceed this limit, the **oldest** packet must be removed to free up space. |
| 16 | + |
| 17 | +` bool addPacket(int source, int destination, int timestamp)`: Adds a packet with the given attributes to the router. |
| 18 | + |
| 19 | +- A packet is considered a duplicate if another packet with the same `source`, `destination`, and `timestamp` already exists in the router. |
| 20 | +- Return `true` if the packet is successfully added (i.e., it is not a duplicate); otherwise return `false`. |
| 21 | + |
| 22 | +` int[] forwardPacket()`: Forwards the next packet in FIFO (First In First Out) order. |
| 23 | + |
| 24 | +- Remove the packet from storage. |
| 25 | +- Return the packet as an array `[source, destination, timestamp]`. |
| 26 | +- If there are no packets to forward, return an empty array. |
| 27 | + |
| 28 | +`int getCount(int destination, int startTime, int endTime)`: |
| 29 | + |
| 30 | +- 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]`. |
| 31 | + |
| 32 | +**Note** that queries for `addPacket` will be made in increasing order of `timestamp`. |
7 | 33 |
|
8 | 34 | **Example 1:** |
9 | 35 |
|
10 | 36 | ``` |
11 | | -Input: a = "11", b = "1" |
12 | | -Output: "100" |
13 | | -``` |
| 37 | +Input: |
| 38 | +["Router", "addPacket", "addPacket", "addPacket", "addPacket", "addPacket", "forwardPacket", "addPacket", "getCount"] |
| 39 | +[[3], [1, 4, 90], [2, 5, 90], [1, 4, 90], [3, 5, 95], [4, 5, 105], [], [5, 2, 110], [5, 100, 110]] |
14 | 40 |
|
15 | | -## 题意 |
16 | | -> ... |
| 41 | +Output: |
| 42 | +[null, true, true, false, true, true, [2, 5, 90], true, 1] |
17 | 43 |
|
18 | | -## 题解 |
| 44 | +Explanation |
19 | 45 |
|
20 | | -### 思路1 |
21 | | -> ... |
22 | | -Implement Router |
23 | | -```go |
| 46 | +Router router = new Router(3); // Initialize Router with memoryLimit of 3. |
| 47 | +router.addPacket(1, 4, 90); // Packet is added. Return True. |
| 48 | +router.addPacket(2, 5, 90); // Packet is added. Return True. |
| 49 | +router.addPacket(1, 4, 90); // This is a duplicate packet. Return False. |
| 50 | +router.addPacket(3, 5, 95); // Packet is added. Return True |
| 51 | +router.addPacket(4, 5, 105); // Packet is added, [1, 4, 90] is removed as number of packets exceeds memoryLimit. Return True. |
| 52 | +router.forwardPacket(); // Return [2, 5, 90] and remove it from router. |
| 53 | +router.addPacket(5, 2, 110); // Packet is added. Return True. |
| 54 | +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. |
24 | 55 | ``` |
25 | 56 |
|
| 57 | +**Example 2:** |
| 58 | + |
| 59 | +``` |
| 60 | +Input: |
| 61 | +["Router", "addPacket", "forwardPacket", "forwardPacket"] |
| 62 | +[[2], [7, 4, 90], [], []] |
| 63 | +
|
| 64 | +Output: |
| 65 | +[null, true, [7, 4, 90], []] |
| 66 | +
|
| 67 | +Explanation |
| 68 | +
|
| 69 | +Router router = new Router(2); // Initialize Router with memoryLimit of 2. |
| 70 | +router.addPacket(7, 4, 90); // Return True. |
| 71 | +router.forwardPacket(); // Return [7, 4, 90]. |
| 72 | +router.forwardPacket(); // There are no packets left, return []. |
| 73 | +``` |
26 | 74 |
|
27 | 75 | ## 结语 |
28 | 76 |
|
|
0 commit comments