-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path3508-implement-router.js
More file actions
166 lines (144 loc) · 4.94 KB
/
3508-implement-router.js
File metadata and controls
166 lines (144 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* 3508. Implement Router
* https://leetcode.com/problems/implement-router/
* Difficulty: Medium
*
* 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 router can store at any given time.
* - If adding a new packet would exceed this limit, the oldest packet must be removed to
* free up space.
* bool addPacket(int source, int destination, int timestamp): Adds a packet with the
* given attributes to the router.
* - A packet is considered a duplicate if another packet with the same source, destination,
* and timestamp already exists in the router.
* - Return true if the packet is successfully added (i.e., it is not a duplicate); otherwise
* return false.
* int[] forwardPacket(): Forwards the next packet in FIFO (First In First Out) order.
* - Remove the packet from storage.
* - Return the packet as an array [source, destination, timestamp].
* - If there are no packets to forward, return an empty array.
* int getCount(int destination, int startTime, int endTime):
* - 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].
*
* Note that queries for addPacket will be made in increasing order of timestamp.
*/
/**
* @param {number} memoryLimit
*/
var Router = function(memoryLimit) {
this.memoryCapacity = memoryLimit;
this.packetQueue = [];
this.packetSet = new Set();
this.destinationTimestamps = new Map();
this.removedPacketIndex = new Map();
};
/**
* @param {number} source
* @param {number} destination
* @param {number} timestamp
* @return {boolean}
*/
Router.prototype.addPacket = function(source, destination, timestamp) {
const packetKey = `${source}-${destination}-${timestamp}`;
if (this.packetSet.has(packetKey)) {
return false;
}
if (this.packetQueue.length === this.memoryCapacity) {
const [oldSource, oldDestination, oldTimestamp] = this.packetQueue.shift();
const oldPacketKey = `${oldSource}-${oldDestination}-${oldTimestamp}`;
this.packetSet.delete(oldPacketKey);
this.removedPacketIndex.set(
oldDestination,
(this.removedPacketIndex.get(oldDestination) || 0) + 1,
);
}
this.packetQueue.push([source, destination, timestamp]);
this.packetSet.add(packetKey);
if (!this.destinationTimestamps.has(destination)) {
this.destinationTimestamps.set(destination, []);
}
this.destinationTimestamps.get(destination).push(timestamp);
return true;
};
/**
* @return {number[]}
*/
Router.prototype.forwardPacket = function() {
if (this.packetQueue.length === 0) {
return [];
}
const [source, destination, timestamp] = this.packetQueue.shift();
const packetKey = `${source}-${destination}-${timestamp}`;
this.packetSet.delete(packetKey);
this.removedPacketIndex.set(destination, (this.removedPacketIndex.get(destination) || 0) + 1);
return [source, destination, timestamp];
};
/**
* @param {number} destination
* @param {number} startTime
* @param {number} endTime
* @return {number}
*/
Router.prototype.getCount = function(destination, startTime, endTime) {
if (!this.destinationTimestamps.has(destination)) {
return 0;
}
const timestampArray = this.destinationTimestamps.get(destination);
const removedCount = this.removedPacketIndex.get(destination) || 0;
const totalLength = timestampArray.length;
if (removedCount >= totalLength) {
return 0;
}
const leftBound = this.binarySearchLeft(timestampArray, startTime, removedCount);
const rightBound = this.binarySearchRight(timestampArray, endTime, removedCount) - 1;
if (leftBound > rightBound) {
return 0;
}
return rightBound - leftBound + 1;
};
/**
* @param {number[]} array
* @param {number} target
* @param {number} startIndex
* @return {number}
*/
Router.prototype.binarySearchLeft = function(array, target, startIndex) {
let left = startIndex;
let right = array.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (array[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
};
/**
* @param {number[]} array
* @param {number} target
* @param {number} startIndex
* @return {number}
*/
Router.prototype.binarySearchRight = function(array, target, startIndex) {
let left = startIndex;
let right = array.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (array[mid] <= target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
};