3508. Implement Router Medium

@problem@discussion
#Array#Hash Table#Binary Search#Design#Queue#Ordered Set



1/**
2 * [3508] Implement Router
3 *
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 * Router(int memoryLimit): Initializes the Router object with a fixed memory limit.
12 * 
13 * 	memoryLimit is the maximum number of packets the router can store at any given time.
14 * 	If adding a new packet would exceed this limit, the oldest packet must be removed to free up space.
15 * 
16 * bool addPacket(int source, int destination, int timestamp): Adds a packet with the given attributes to the router.
17 * 
18 * 	A packet is considered a duplicate if another packet with the same source, destination, and timestamp already exists in the router.
19 * 	Return true if the packet is successfully added (i.e., it is not a duplicate); otherwise return false.
20 * 
21 * int[] forwardPacket(): Forwards the next packet in FIFO (First In First Out) order.
22 * 
23 * 	Remove the packet from storage.
24 * 	Return the packet as an array [source, destination, timestamp].
25 * 	If there are no packets to forward, return an empty array.
26 * 
27 * int getCount(int destination, int startTime, int endTime):
28 * 
29 * 	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].
30 * 
31 * Note that queries for addPacket will be made in non-decreasing order of timestamp.
32 *  
33 * <strong class="example">Example 1:
34 * <div class="example-block">
35 * Input:<br />
36 * <span class="example-io">["Router", "addPacket", "addPacket", "addPacket", "addPacket", "addPacket", "forwardPacket", "addPacket", "getCount"]<br />
37 * [[3], [1, 4, 90], [2, 5, 90], [1, 4, 90], [3, 5, 95], [4, 5, 105], [], [5, 2, 110], [5, 100, 110]]</span>
38 * Output:<br />
39 * <span class="example-io">[null, true, true, false, true, true, [2, 5, 90], true, 1] </span>
40 * Explanation
41 * Router router = new Router(3); // Initialize Router with memoryLimit of 3.<br />
42 * router.addPacket(1, 4, 90); // Packet is added. Return True.<br />
43 * router.addPacket(2, 5, 90); // Packet is added. Return True.<br />
44 * router.addPacket(1, 4, 90); // This is a duplicate packet. Return False.<br />
45 * router.addPacket(3, 5, 95); // Packet is added. Return True<br />
46 * router.addPacket(4, 5, 105); // Packet is added, [1, 4, 90] is removed as number of packets exceeds memoryLimit. Return True.<br />
47 * router.forwardPacket(); // Return [2, 5, 90] and remove it from router.<br />
48 * router.addPacket(5, 2, 110); // Packet is added. Return True.<br />
49 * 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.</div>
50 * <strong class="example">Example 2:
51 * <div class="example-block">
52 * Input:<br />
53 * <span class="example-io">["Router", "addPacket", "forwardPacket", "forwardPacket"]<br />
54 * [[2], [7, 4, 90], [], []]</span>
55 * Output:<br />
56 * <span class="example-io">[null, true, [7, 4, 90], []] </span>
57 * Explanation
58 * Router router = new Router(2); // Initialize Router with memoryLimit of 2.<br />
59 * router.addPacket(7, 4, 90); // Return True.<br />
60 * router.forwardPacket(); // Return [7, 4, 90].<br />
61 * router.forwardPacket(); // There are no packets left, return [].</div>
62 *  
63 * Constraints:
64 * 
65 * 	2 <= memoryLimit <= 10^5
66 * 	1 <= source, destination <= 2 * 10^5
67 * 	1 <= timestamp <= 10^9
68 * 	1 <= startTime <= endTime <= 10^9
69 * 	At most 10^5 calls will be made to addPacket, forwardPacket, and getCount methods altogether.
70 * 	queries for addPacket will be made in non-decreasing order of timestamp.
71 * 
72 */
73pub struct Solution {}
74
75// problem: https://leetcode.com/problems/implement-router/
76// discuss: https://leetcode.com/problems/implement-router/discuss/?currentPage=1&orderBy=most_votes&query=
77
78// submission codes start here
79
80struct Router {
81        false
82    }
83
84
85/** 
86 * `&self` means the method takes an immutable reference.
87 * If you need a mutable reference, change it to `&mut self` instead.
88 */
89impl Router {
90
91    fn new(memoryLimit: i32) -> Self {
92        
93    }
94    
95    fn add_packet(&self, source: i32, destination: i32, timestamp: i32) -> bool {
96        
97    }
98    
99    fn forward_packet(&self) -> Vec<i32> {
100        
101    }
102    
103    fn get_count(&self, destination: i32, start_time: i32, end_time: i32) -> i32 {
104        
105    }
106}
107
108/**
109 * Your Router object will be instantiated and called as such:
110 * let obj = Router::new(memoryLimit);
111 * let ret_1: bool = obj.add_packet(source, destination, timestamp);
112 * let ret_2: Vec<i32> = obj.forward_packet();
113 * let ret_3: i32 = obj.get_count(destination, startTime, endTime);
114 */
115
116// submission codes end
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn test_3508() {
124    }
125}
126

Back
© 2026 bowen.ge All Rights Reserved.