LeetCode 3508: Implement Router Solution
Master LeetCode problem 3508 (Implement Router), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3508. Implement Router
Problem Explanation
Explanation:
To solve this problem, we can use a combination of data structures to efficiently manage the data packets in the router. We can use a HashMap to store the packets based on their source, destination, and timestamp. Additionally, we can use a Queue to maintain the order of packets based on their arrival time.
-
addPacket(int source, int destination, int timestamp):
- Check if the packet is a duplicate by looking it up in the HashMap.
- If it's not a duplicate, add the packet to the HashMap and the Queue.
- If adding the new packet exceeds the memory limit, remove the oldest packet from both the HashMap and the Queue.
-
forwardPacket():
- Remove the next packet in FIFO order from the Queue.
- Return the packet information as an array.
-
getCount(int destination, int startTime, int endTime):
- Iterate through the packets in the HashMap with the specified destination.
- Count the packets that have timestamps in the inclusive range [startTime, endTime].
:
Solution Code
import java.util.*;
class Router {
int memoryLimit;
Map<String, Integer[]> packetsMap;
Queue<Integer[]> packetsQueue;
public Router(int memoryLimit) {
this.memoryLimit = memoryLimit;
packetsMap = new HashMap<>();
packetsQueue = new LinkedList<>();
}
public boolean addPacket(int source, int destination, int timestamp) {
String key = source + "-" + destination + "-" + timestamp;
if (packetsMap.containsKey(key)) {
return false;
}
Integer[] packet = {source, destination, timestamp};
packetsMap.put(key, packet);
packetsQueue.offer(packet);
if (packetsQueue.size() > memoryLimit) {
Integer[] oldestPacket = packetsQueue.poll();
packetsMap.remove(oldestPacket[0] + "-" + oldestPacket[1] + "-" + oldestPacket[2]);
}
return true;
}
public int[] forwardPacket() {
if (packetsQueue.isEmpty()) {
return new int[]{};
}
Integer[] packet = packetsQueue.poll();
packetsMap.remove(packet[0] + "-" + packet[1] + "-" + packet[2]);
return new int[]{packet[0], packet[1], packet[2]};
}
public int getCount(int destination, int startTime, int endTime) {
int count = 0;
for (Integer[] packet : packetsMap.values()) {
if (packet[1] == destination && packet[2] >= startTime && packet[2] <= endTime) {
count++;
}
}
return count;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3508 (Implement Router)?
This page provides optimized solutions for LeetCode problem 3508 (Implement Router) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3508 (Implement Router)?
The time complexity for LeetCode 3508 (Implement Router) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3508 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3508 in Java, C++, or Python.