Advertisement

Implement Router - LeetCode 3508 Solution

Implement Router - Complete Solution Guide

Implement Router is LeetCode problem 3508, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes: source : A unique identifier for the machine that generated the packet. destination : A unique identifier for the target machine. timestamp : The time at which the packet arrived at the router. Implement the Router class: Router(int memoryLimit) : Initializes the Router object with a fixed memory limit. memoryLimit is the maximum number of packets the route

Detailed Explanation

The problem asks us to implement a `Router` class that simulates a network router managing data packets. Each packet has a `source`, `destination`, and `timestamp`. The router has a fixed `memoryLimit` on the number of packets it can store. The `Router` class needs to support four operations: `addPacket` (adds a new packet, returning true if added successfully and false if it's a duplicate), `forwardPacket` (removes and returns the oldest packet), `getCount` (returns the number of packets with a specific destination and timestamp within a range), and the constructor `Router(memoryLimit)`. `addPacket` operations arrive in increasing order of timestamp. When the router's memory is full, the oldest packet should be removed before adding a new one.

Solution Approach

The solution uses a combination of a queue, a hash set, and a hash map to maintain the packets and their timestamps. A deque (double-ended queue) is used to store the packets in FIFO order, which is important for the `forwardPacket` operation. A set is used to quickly check for duplicates when adding a new packet, providing O(1) lookup time. A dictionary (hash map) is used to maintain information about each destination, namely the timestamps and a start index. The start index optimization prevents rescanning of removed packet indices, by saving the 'first index' of valid timstamps in the `getCount` function.

Step-by-Step Algorithm

  1. Step 1: Initialize the Router with a given `memoryLimit`, a deque (`packets_queue`), a set (`packets_set`), and a dictionary (`dest_info`).
  2. Step 2: In `addPacket`, check if the packet already exists in `packets_set`. If it does, return `false`.
  3. Step 3: If the router is at its `memoryLimit`, remove the oldest packet from `packets_queue` and `packets_set`. Decrement `start_idx` for the desitnation of removed packet.
  4. Step 4: Add the new packet to `packets_queue` and `packets_set`. Also, append the packet's timestamp to the list of timestamps associated with the packet's destination in `dest_info`.
  5. Step 5: In `forwardPacket`, if `packets_queue` is empty, return an empty list. Otherwise, remove the oldest packet from the queue and set, increment `start_idx` for the desitnation of removed packet and return its attributes as a list.
  6. Step 6: In `getCount`, if the destination is not in `dest_info`, return 0. Otherwise, retrieve the timestamps list and the starting index associated with the destination. Perform binary search to find the indices of the first timestamp greater than or equal to `startTime` and the first timestamp greater than `endTime`. The difference between these indices gives the count of packets within the specified range.
  7. Step 7: The C implementation uses a circular array as the queue and separate hash tables for the packets_set and dest_info. The dest_info also tracks timestamp arrays for each destination.

Key Insights

  • Insight 1: Use a queue (collections.deque in Python, deque in C++, circular array in C) to maintain the FIFO order of packets for forwarding.
  • Insight 2: Use a hash set (set in Python and C++, custom hash table in C) to quickly check for duplicate packets based on source, destination and timestamp. This avoids linear search.
  • Insight 3: For efficient `getCount` operation, maintain sorted lists of timestamps for each destination. This allows the use of binary search to count packets within the specified timestamp range.
  • Insight 4: To avoid recalculating the index where valid timestamps start for a destination, maintain an offset (`start_idx`) to mark the beginning of valid timestamp entries in the sorted timestamp list for a particular destination.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(m)

Topics

This problem involves: Array, Hash Table, Binary Search, Design, Queue, Ordered Set.

Companies

Asked at: Cisco.