Press "Enter" to skip to content

花花酱 LeetCode 3508. Implement Router

算是比较复杂的题目了,主要考察数据结构的选择和应用。

Note that queries for addPacket will be made in increasing order of timestamp.
把题目简化了不少,注意timestamp可能会想同,非严格单调递增。

forwardPacket 的 FIFO,需要deque或者list
getCount 特定key的范围查询,hashmap + vector / deque
addPacket 需要用hashtable 去重。还要remove最旧的packet,最后的选择落到的deque上。

deque两头可以添加/删除,还能random access做二分搜索,真是居家旅行必备之数据结构。

时间复杂度:addPacket: O(1),forwardPacket:O(1),getCount:O(logN) N为dst的packet数量。
空间复杂度:O(M)

请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.

Buy anything from Amazon to support our website
您可以通过在亚马逊上购物(任意商品)来支持我们

Paypal
Venmo
huahualeetcode
微信打赏

Be First to Comment

Leave a Reply