Title: #3508 - Implement Router - LeetCode JavaScript Solutions
Open Graph Title: #3508 - Implement Router - LeetCode JavaScript Solutions
X Title: #3508 - Implement Router - LeetCode JavaScript Solutions
Description: Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes:...
Open Graph Description: Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes:...
X Description: Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes:...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/implement-router
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/implement-router","name":"#3508 - Implement Router - LeetCode JavaScript Solutions","description":"Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes:...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} memoryLimit\n */\nvar Router = function(memoryLimit) {\n this.memoryCapacity = memoryLimit;\n this.packetQueue = [];\n this.packetSet = new Set();\n this.destinationTimestamps = new Map();\n this.removedPacketIndex = new Map();\n};\n\n/**\n * @param {number} source\n * @param {number} destination\n * @param {number} timestamp\n * @return {boolean}\n */\nRouter.prototype.addPacket = function(source, destination, timestamp) {\n const packetKey = `${source}-${destination}-${timestamp}`;\n\n if (this.packetSet.has(packetKey)) {\n return false;\n }\n\n if (this.packetQueue.length === this.memoryCapacity) {\n const [oldSource, oldDestination, oldTimestamp] = this.packetQueue.shift();\n const oldPacketKey = `${oldSource}-${oldDestination}-${oldTimestamp}`;\n this.packetSet.delete(oldPacketKey);\n this.removedPacketIndex.set(\n oldDestination,\n (this.removedPacketIndex.get(oldDestination) || 0) + 1,\n );\n }\n\n this.packetQueue.push([source, destination, timestamp]);\n this.packetSet.add(packetKey);\n\n if (!this.destinationTimestamps.has(destination)) {\n this.destinationTimestamps.set(destination, []);\n }\n this.destinationTimestamps.get(destination).push(timestamp);\n\n return true;\n};\n\n/**\n * @return {number[]}\n */\nRouter.prototype.forwardPacket = function() {\n if (this.packetQueue.length === 0) {\n return [];\n }\n\n const [source, destination, timestamp] = this.packetQueue.shift();\n const packetKey = `${source}-${destination}-${timestamp}`;\n this.packetSet.delete(packetKey);\n this.removedPacketIndex.set(destination, (this.removedPacketIndex.get(destination) || 0) + 1);\n\n return [source, destination, timestamp];\n};\n\n/**\n * @param {number} destination\n * @param {number} startTime\n * @param {number} endTime\n * @return {number}\n */\nRouter.prototype.getCount = function(destination, startTime, endTime) {\n if (!this.destinationTimestamps.has(destination)) {\n return 0;\n }\n\n const timestampArray = this.destinationTimestamps.get(destination);\n const removedCount = this.removedPacketIndex.get(destination) || 0;\n const totalLength = timestampArray.length;\n\n if (removedCount >= totalLength) {\n return 0;\n }\n\n const leftBound = this.binarySearchLeft(timestampArray, startTime, removedCount);\n const rightBound = this.binarySearchRight(timestampArray, endTime, removedCount) - 1;\n\n if (leftBound > rightBound) {\n return 0;\n }\n\n return rightBound - leftBound + 1;\n};\n\n/**\n * @param {number[]} array\n * @param {number} target\n * @param {number} startIndex\n * @return {number}\n */\nRouter.prototype.binarySearchLeft = function(array, target, startIndex) {\n let left = startIndex;\n let right = array.length;\n\n while (left < right) {\n const mid = Math.floor((left + right) / 2);\n if (array[mid] < target) {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n\n return left;\n};\n\n/**\n * @param {number[]} array\n * @param {number} target\n * @param {number} startIndex\n * @return {number}\n */\nRouter.prototype.binarySearchRight = function(array, target, startIndex) {\n let left = startIndex;\n let right = array.length;\n\n while (left < right) {\n const mid = Math.floor((left + right) / 2);\n if (array[mid] <= target) {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n\n return left;\n};","keywords":"LeetCode 3508, #3508 - Implement Router, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/implement-router |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/implement-router/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/3508-implement-router.js |
| Array | https://leetcodejavascript.com/tags/array |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| Binary Search | https://leetcodejavascript.com/tags/binary-search |
| Design | https://leetcodejavascript.com/tags/design |
| Ordered Set | https://leetcodejavascript.com/tags/ordered-set |
| Queue | https://leetcodejavascript.com/tags/queue |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow