|
| 1 | +--- |
| 2 | +title: 3508.设计路由器:STL套STL——有什么需求就设计什么数据结构 |
| 3 | +date: 2025-09-20 17:40:53 |
| 4 | +tags: [题解, LeetCode, 中等, 设计, 队列, 数组, 哈希表, set, map, 二分查找, 有序集合] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】3508.设计路由器:STL套STL——有什么需求就设计什么数据结构 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/implement-router/](https://leetcode.cn/problems/implement-router/) |
| 11 | + |
| 12 | +<p>请你设计一个数据结构来高效管理网络路由器中的数据包。每个数据包包含以下属性:</p> |
| 13 | + |
| 14 | +<ul> |
| 15 | + <li><code>source</code>:生成该数据包的机器的唯一标识符。</li> |
| 16 | + <li><code>destination</code>:目标机器的唯一标识符。</li> |
| 17 | + <li><code>timestamp</code>:该数据包到达路由器的时间戳。</li> |
| 18 | +</ul> |
| 19 | + |
| 20 | +<p>实现 <code>Router</code> 类:</p> |
| 21 | + |
| 22 | +<p><code>Router(int memoryLimit)</code>:初始化路由器对象,并设置固定的内存限制。</p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li><code>memoryLimit</code> 是路由器在任意时间点可以存储的 <strong>最大</strong> 数据包数量。</li> |
| 26 | + <li>如果添加一个新数据包会超过这个限制,则必须移除 <strong>最旧的</strong> 数据包以腾出空间。</li> |
| 27 | +</ul> |
| 28 | + |
| 29 | +<p><code>bool addPacket(int source, int destination, int timestamp)</code>:将具有给定属性的数据包添加到路由器。</p> |
| 30 | + |
| 31 | +<ul> |
| 32 | + <li>如果路由器中已经存在一个具有相同 <code>source</code>、<code>destination</code> 和 <code>timestamp</code> 的数据包,则视为重复数据包。</li> |
| 33 | + <li>如果数据包成功添加(即不是重复数据包),返回 <code>true</code>;否则返回 <code>false</code>。</li> |
| 34 | +</ul> |
| 35 | + |
| 36 | +<p><code>int[] forwardPacket()</code>:以 FIFO(先进先出)顺序转发下一个数据包。</p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li>从存储中移除该数据包。</li> |
| 40 | + <li>以数组 <code>[source, destination, timestamp]</code> 的形式返回该数据包。</li> |
| 41 | + <li>如果没有数据包可以转发,则返回空数组。</li> |
| 42 | +</ul> |
| 43 | + |
| 44 | +<p><code>int getCount(int destination, int startTime, int endTime)</code>:</p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li>返回当前存储在路由器中(即尚未转发)的,且目标地址为指定 <code>destination</code> 且时间戳在范围 <code>[startTime, endTime]</code>(包括两端)内的数据包数量。</li> |
| 48 | +</ul> |
| 49 | + |
| 50 | +<p><strong>注意</strong>:对于 <code>addPacket</code> 的查询会按照 <code>timestamp</code> 的递增顺序进行。</p> |
| 51 | + |
| 52 | +<p> </p> |
| 53 | + |
| 54 | +<p><strong class="example">示例 1:</strong></p> |
| 55 | + |
| 56 | +<div class="example-block"> |
| 57 | +<p><strong>输入:</strong><br /> |
| 58 | +<span class="example-io">["Router", "addPacket", "addPacket", "addPacket", "addPacket", "addPacket", "forwardPacket", "addPacket", "getCount"]<br /> |
| 59 | +[[3], [1, 4, 90], [2, 5, 90], [1, 4, 90], [3, 5, 95], [4, 5, 105], [], [5, 2, 110], [5, 100, 110]]</span></p> |
| 60 | + |
| 61 | +<p><strong>输出:</strong><br /> |
| 62 | +<span class="example-io">[null, true, true, false, true, true, [2, 5, 90], true, 1] </span></p> |
| 63 | + |
| 64 | +<p><strong>解释:</strong></p> |
| 65 | +<code>Router router = new Router(3);</code> // 初始化路由器,内存限制为 3。<br /> |
| 66 | +<code>router.addPacket(1, 4, 90);</code> // 数据包被添加,返回 True。<br /> |
| 67 | +<code>router.addPacket(2, 5, 90);</code> // 数据包被添加,返回 True。<br /> |
| 68 | +<code>router.addPacket(1, 4, 90);</code> // 这是一个重复数据包,返回 False。<br /> |
| 69 | +<code>router.addPacket(3, 5, 95);</code> // 数据包被添加,返回 True。<br /> |
| 70 | +<code>router.addPacket(4, 5, 105);</code> // 数据包被添加,<code>[1, 4, 90]</code> 被移除,因为数据包数量超过限制,返回 True。<br /> |
| 71 | +<code>router.forwardPacket();</code> // 转发数据包 <code>[2, 5, 90]</code> 并将其从路由器中移除。<br /> |
| 72 | +<code>router.addPacket(5, 2, 110);</code> // 数据包被添加,返回 True。<br /> |
| 73 | +<code>router.getCount(5, 100, 110);</code> // 唯一目标地址为 5 且时间在 <code>[100, 110]</code> 范围内的数据包是 <code>[4, 5, 105]</code>,返回 1。</div> |
| 74 | + |
| 75 | +<p><strong class="example">示例 2:</strong></p> |
| 76 | + |
| 77 | +<div class="example-block"> |
| 78 | +<p><strong>输入:</strong><br /> |
| 79 | +<span class="example-io">["Router", "addPacket", "forwardPacket", "forwardPacket"]<br /> |
| 80 | +[[2], [7, 4, 90], [], []]</span></p> |
| 81 | + |
| 82 | +<p><strong>输出:</strong><br /> |
| 83 | +<span class="example-io">[null, true, [7, 4, 90], []] </span></p> |
| 84 | + |
| 85 | +<p><strong>解释:</strong></p> |
| 86 | +<code>Router router = new Router(2);</code> // 初始化路由器,内存限制为 2。<br /> |
| 87 | +<code>router.addPacket(7, 4, 90);</code> // 返回 True。<br /> |
| 88 | +<code>router.forwardPacket();</code> // 返回 <code>[7, 4, 90]</code>。<br /> |
| 89 | +<code>router.forwardPacket();</code> // 没有数据包可以转发,返回 <code>[]</code>。</div> |
| 90 | + |
| 91 | +<p> </p> |
| 92 | + |
| 93 | +<p><strong>提示:</strong></p> |
| 94 | + |
| 95 | +<ul> |
| 96 | + <li><code>2 <= memoryLimit <= 10<sup>5</sup></code></li> |
| 97 | + <li><code>1 <= source, destination <= 2 * 10<sup>5</sup></code></li> |
| 98 | + <li><code>1 <= timestamp <= 10<sup>9</sup></code></li> |
| 99 | + <li><code>1 <= startTime <= endTime <= 10<sup>9</sup></code></li> |
| 100 | + <li><code>addPacket</code>、<code>forwardPacket</code> 和 <code>getCount</code> 方法的总调用次数最多为 <code>10<sup>5</sup></code>。</li> |
| 101 | + <li>对于 <code>addPacket</code> 的查询,<code>timestamp</code> 按递增顺序给出。</li> |
| 102 | +</ul> |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +## 解题方法:STL套STL |
| 107 | + |
| 108 | +首先拆解需求,无非以下三个: |
| 109 | + |
| 110 | +1. FIFO; |
| 111 | +2. 判断一个packet是否已经存在; |
| 112 | +3. 查找指定destination的所有包中时间戳在`[s, e]`中的有多少个。 |
| 113 | + |
| 114 | +让我们逐个分析破解之,有什么需求就设计什么数据结构。 |
| 115 | + |
| 116 | +**FIFO** |
| 117 | + |
| 118 | +首先对于第一个需求,当路由器中数据包数量已经达到memoryLimit个时,新来的数据包会挤掉最早加入的数据包。 |
| 119 | + |
| 120 | +很简单,使用一个队列就行。如果想效率更高点,也可以使用数组模拟队列(容量多开一个来区分队列是满是空,两个指针指向队列第一个元素下标和最后一个元素下标的下一个下标)。 |
| 121 | + |
| 122 | +由于使用了多个数据结构,所以有元素出队时记得更新其他数据结构中的内容。 |
| 123 | + |
| 124 | +**判断一个packet是否已经存在** |
| 125 | + |
| 126 | +这个需求来自新加入包时是返回true还是false。这个也简单,使用一个哈希表判断路由器中都有哪些包就好了。 |
| 127 | + |
| 128 | +**查找指定destination的所有包中时间戳在`[s, e]`中的有多少个** |
| 129 | + |
| 130 | +这个相对复杂一点。所好在于,题目给定数据满足添加addPacket的timestamp是递增的。 |
| 131 | + |
| 132 | +> 最后那句“注意”我也没看懂,也不知道到底是同destination的包timestamp递增还是所有包整体上timestamp递增,反正无所谓,就当成“针对这道题随便操作,只要你所涉及到的数据包都是timestamp递增的”就好了。 |
| 133 | +
|
| 134 | +好吧,既然给定的数据包都是timestamp递增的了,那么范围查询就容易实现了。 |
| 135 | + |
| 136 | +既然查询的范围是指定的destination,那么我们就简历一个哈希表finder,把键设置为destination好了。finder[destination]是所有目标地址为destination的数据包。 |
| 137 | + |
| 138 | +这些数据包怎么存储呢?使用一个vector存放每个数据包的timestamp就可以了,新来数据包就直接push到vector后面,就能实现vector中的数据包是天然按照timestamp递增的,就能使用二分法在log级别的时间内找到时间戳在`[s, e]`中的数据包有多少个了。 |
| 139 | + |
| 140 | +这就解决了?等下,数据包还能被移除呢!怎么办,好说,再使用一个整数startIdx记录vector中没被移除的数据包下标是从哪个开始的就好了。 |
| 141 | + |
| 142 | +> 相当于移除数据包时,在针对需求3设计的结构体中,并没有真正移除,而是把“有效范围指针”右移了一位。实际设计中可别这样!内存分分钟爆炸。 |
| 143 | +
|
| 144 | +### 时空复杂度分析 |
| 145 | + |
| 146 | ++ 时间复杂度:add和forward $O(1)$,count $(\log memoryLimit)$ |
| 147 | ++ 空间复杂度$O(memoryLimit)$ |
| 148 | + |
| 149 | +### AC代码 |
| 150 | + |
| 151 | +#### C++ |
| 152 | + |
| 153 | +```cpp |
| 154 | +/* |
| 155 | + * @Author: LetMeFly |
| 156 | + * @Date: 2025-09-20 16:10:20 |
| 157 | + * @LastEditors: LetMeFly.xyz |
| 158 | + * @LastEditTime: 2025-09-20 17:38:56 |
| 159 | + */ |
| 160 | +#if defined(_WIN32) || defined(__APPLE__) |
| 161 | +#include "_[1,2]toVector.h" |
| 162 | +#endif |
| 163 | + |
| 164 | +/* |
| 165 | +FIFO: 一个数组一个指针 |
| 166 | +是否重复:一个哈希表 |
| 167 | +Find:哈希表+有序集合 |
| 168 | +
|
| 169 | +啊,我不想写了 |
| 170 | +*/ |
| 171 | +inline void hash_combine(size_t &seed, size_t h) { |
| 172 | + seed ^= h + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2); |
| 173 | +} |
| 174 | + |
| 175 | +struct TupleHash { |
| 176 | + size_t operator()(const tuple<int, int, int>& t) const noexcept { |
| 177 | + size_t seed = 0; |
| 178 | + hash_combine(seed, hash<int>{}(get<0>(t))); |
| 179 | + hash_combine(seed, hash<int>{}(get<1>(t))); |
| 180 | + hash_combine(seed, hash<int>{}(get<2>(t))); |
| 181 | + return seed; |
| 182 | + } |
| 183 | +}; |
| 184 | + |
| 185 | +struct TupleEq { |
| 186 | + bool operator()(const tuple<int, int, int>& a, const tuple<int, int, int>& b) const noexcept { // 记得const noexcept(好难记) |
| 187 | + return a == b; |
| 188 | + } |
| 189 | +}; |
| 190 | + |
| 191 | +class Router { |
| 192 | +private: |
| 193 | + vector<tuple<int, int, int>> fifo; |
| 194 | + int fifoLeft, fifoRight, memoryLimit; |
| 195 | + unordered_set<tuple<int, int, int>, TupleHash, TupleEq> already; |
| 196 | + unordered_map<int, pair<vector<int>, int>> finder; // destance -> <[timestamp], 有效范围> |
| 197 | + |
| 198 | + void removePacket() { // remove fifoLeft,不判断是否为空 |
| 199 | + tuple<int, int, int> toRemove = fifo[fifoLeft]; |
| 200 | + already.erase(toRemove); |
| 201 | + finder[get<1>(toRemove)].second++; |
| 202 | + fifoLeft = (fifoLeft + 1) % (memoryLimit + 1); |
| 203 | + } |
| 204 | +public: |
| 205 | + Router(int memoryLimit) { |
| 206 | + this->memoryLimit = memoryLimit; |
| 207 | + fifo.resize(memoryLimit + 1); // 多开一个区分空和满 |
| 208 | + fifoLeft = fifoRight = 0; |
| 209 | + } |
| 210 | + |
| 211 | + bool addPacket(int source, int destination, int timestamp) { |
| 212 | + tuple<int, int, int> package = tuple(source, destination, timestamp); |
| 213 | + if (already.count(package)) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + already.insert(package); |
| 217 | + if ((fifoRight + 1) % (memoryLimit + 1) == fifoLeft) { // remove fifo[fifoLeft] |
| 218 | + removePacket(); |
| 219 | + } |
| 220 | + fifo[fifoRight] = package; |
| 221 | + fifoRight = (fifoRight + 1) % (memoryLimit + 1); |
| 222 | + finder[destination].first.push_back(timestamp); |
| 223 | + return true; |
| 224 | + } |
| 225 | + |
| 226 | + vector<int> forwardPacket() { |
| 227 | + if (fifoLeft == fifoRight) { |
| 228 | + return {}; |
| 229 | + } |
| 230 | + auto [source, destination, timeStamp] = fifo[fifoLeft]; |
| 231 | + removePacket(); |
| 232 | + return {source, destination, timeStamp}; |
| 233 | + } |
| 234 | + |
| 235 | + int getCount(int destination, int startTime, int endTime) { |
| 236 | + auto&& [timeStamps, startIdx] = finder[destination]; |
| 237 | + return upper_bound(timeStamps.begin() + startIdx, timeStamps.end(), endTime) - lower_bound(timeStamps.begin() + startIdx, timeStamps.end(), startTime); |
| 238 | + } |
| 239 | +}; |
| 240 | + |
| 241 | +/** |
| 242 | + * Your Router object will be instantiated and called as such: |
| 243 | + * Router* obj = new Router(memoryLimit); |
| 244 | + * bool param_1 = obj->addPacket(source,destination,timestamp); |
| 245 | + * vector<int> param_2 = obj->forwardPacket(); |
| 246 | + * int param_3 = obj->getCount(destination,startTime,endTime); |
| 247 | + */ |
| 248 | +``` |
| 249 | +
|
| 250 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/151901838)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/09/20/LeetCode%203508.%E8%AE%BE%E8%AE%A1%E8%B7%AF%E7%94%B1%E5%99%A8/)哦~ |
| 251 | +> |
| 252 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
| 253 | +
|
| 254 | +ios26更新好烫! |
0 commit comments