Skip to content

Commit 5944c37

Browse files
authored
feat: add solutions to lc problem: No.3508 (doocs#4733)
1 parent fd939f6 commit 5944c37

File tree

8 files changed

+1156
-8
lines changed

8 files changed

+1156
-8
lines changed

solution/3500-3599/3508.Implement Router/README.md

Lines changed: 392 additions & 4 deletions
Large diffs are not rendered by default.

solution/3500-3599/3508.Implement Router/README_EN.md

Lines changed: 392 additions & 4 deletions
Large diffs are not rendered by default.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Router {
2+
private:
3+
int lim;
4+
unordered_set<long long> vis;
5+
deque<array<int, 3>> q;
6+
unordered_map<int, int> idx;
7+
unordered_map<int, vector<int>> d;
8+
9+
long long f(int a, int b, int c) {
10+
return ((long long) a << 46) | ((long long) b << 29) | (long long) c;
11+
}
12+
13+
public:
14+
Router(int memoryLimit) {
15+
lim = memoryLimit;
16+
}
17+
18+
bool addPacket(int source, int destination, int timestamp) {
19+
long long x = f(source, destination, timestamp);
20+
if (vis.count(x)) {
21+
return false;
22+
}
23+
vis.insert(x);
24+
if ((int) q.size() >= lim) {
25+
forwardPacket();
26+
}
27+
q.push_back({source, destination, timestamp});
28+
d[destination].push_back(timestamp);
29+
return true;
30+
}
31+
32+
vector<int> forwardPacket() {
33+
if (q.empty()) {
34+
return {};
35+
}
36+
auto packet = q.front();
37+
q.pop_front();
38+
int s = packet[0], d_ = packet[1], t = packet[2];
39+
vis.erase(f(s, d_, t));
40+
idx[d_]++;
41+
return {s, d_, t};
42+
}
43+
44+
int getCount(int destination, int startTime, int endTime) {
45+
auto& ls = d[destination];
46+
int k = idx[destination];
47+
auto i = lower_bound(ls.begin() + k, ls.end(), startTime);
48+
auto j = lower_bound(ls.begin() + k, ls.end(), endTime + 1);
49+
return j - i;
50+
}
51+
};
52+
53+
/**
54+
* Your Router object will be instantiated and called as such:
55+
* Router* obj = new Router(memoryLimit);
56+
* bool param_1 = obj->addPacket(source,destination,timestamp);
57+
* vector<int> param_2 = obj->forwardPacket();
58+
* int param_3 = obj->getCount(destination,startTime,endTime);
59+
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
type Router struct {
2+
lim int
3+
vis map[int64]struct{}
4+
q [][3]int
5+
idx map[int]int
6+
d map[int][]int
7+
}
8+
9+
func Constructor(memoryLimit int) Router {
10+
return Router{
11+
lim: memoryLimit,
12+
vis: make(map[int64]struct{}),
13+
q: make([][3]int, 0),
14+
idx: make(map[int]int),
15+
d: make(map[int][]int),
16+
}
17+
}
18+
19+
func (this *Router) f(a, b, c int) int64 {
20+
return int64(a)<<46 | int64(b)<<29 | int64(c)
21+
}
22+
23+
func (this *Router) AddPacket(source int, destination int, timestamp int) bool {
24+
x := this.f(source, destination, timestamp)
25+
if _, ok := this.vis[x]; ok {
26+
return false
27+
}
28+
this.vis[x] = struct{}{}
29+
if len(this.q) >= this.lim {
30+
this.ForwardPacket()
31+
}
32+
this.q = append(this.q, [3]int{source, destination, timestamp})
33+
this.d[destination] = append(this.d[destination], timestamp)
34+
return true
35+
}
36+
37+
func (this *Router) ForwardPacket() []int {
38+
if len(this.q) == 0 {
39+
return []int{}
40+
}
41+
packet := this.q[0]
42+
this.q = this.q[1:]
43+
s, d, t := packet[0], packet[1], packet[2]
44+
delete(this.vis, this.f(s, d, t))
45+
this.idx[d]++
46+
return []int{s, d, t}
47+
}
48+
49+
func (this *Router) GetCount(destination int, startTime int, endTime int) int {
50+
ls := this.d[destination]
51+
k := this.idx[destination]
52+
i := sort.Search(len(ls)-k, func(i int) bool { return ls[i+k] >= startTime }) + k
53+
j := sort.Search(len(ls)-k, func(i int) bool { return ls[i+k] >= endTime+1 }) + k
54+
return j - i
55+
}
56+
57+
/**
58+
* Your Router object will be instantiated and called as such:
59+
* obj := Constructor(memoryLimit)
60+
* param_1 := obj.AddPacket(source,destination,timestamp)
61+
* param_2 := obj.ForwardPacket()
62+
* param_3 := obj.GetCount(destination,startTime,endTime)
63+
*/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Router {
2+
private int lim;
3+
private Set<Long> vis = new HashSet<>();
4+
private Deque<int[]> q = new ArrayDeque<>();
5+
private Map<Integer, Integer> idx = new HashMap<>();
6+
private Map<Integer, List<Integer>> d = new HashMap<>();
7+
8+
public Router(int memoryLimit) {
9+
this.lim = memoryLimit;
10+
}
11+
12+
public boolean addPacket(int source, int destination, int timestamp) {
13+
long x = f(source, destination, timestamp);
14+
if (vis.contains(x)) {
15+
return false;
16+
}
17+
vis.add(x);
18+
if (q.size() >= lim) {
19+
forwardPacket();
20+
}
21+
q.offer(new int[] {source, destination, timestamp});
22+
d.computeIfAbsent(destination, k -> new ArrayList<>()).add(timestamp);
23+
return true;
24+
}
25+
26+
public int[] forwardPacket() {
27+
if (q.isEmpty()) {
28+
return new int[] {};
29+
}
30+
int[] packet = q.poll();
31+
int s = packet[0], d_ = packet[1], t = packet[2];
32+
vis.remove(f(s, d_, t));
33+
idx.merge(d_, 1, Integer::sum);
34+
return new int[] {s, d_, t};
35+
}
36+
37+
private long f(int a, int b, int c) {
38+
return ((long) a << 46) | ((long) b << 29) | (long) c;
39+
}
40+
41+
public int getCount(int destination, int startTime, int endTime) {
42+
List<Integer> ls = d.getOrDefault(destination, List.of());
43+
int k = idx.getOrDefault(destination, 0);
44+
int i = lowerBound(ls, startTime, k);
45+
int j = lowerBound(ls, endTime + 1, k);
46+
return j - i;
47+
}
48+
49+
private int lowerBound(List<Integer> list, int target, int fromIndex) {
50+
int l = fromIndex, r = list.size();
51+
while (l < r) {
52+
int m = (l + r) >>> 1;
53+
if (list.get(m) < target) {
54+
l = m + 1;
55+
} else {
56+
r = m;
57+
}
58+
}
59+
return l;
60+
}
61+
}
62+
63+
/**
64+
* Your Router object will be instantiated and called as such:
65+
* Router obj = new Router(memoryLimit);
66+
* boolean param_1 = obj.addPacket(source,destination,timestamp);
67+
* int[] param_2 = obj.forwardPacket();
68+
* int param_3 = obj.getCount(destination,startTime,endTime);
69+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Router:
2+
3+
def __init__(self, memoryLimit: int):
4+
self.lim = memoryLimit
5+
self.vis = set()
6+
self.q = deque()
7+
self.idx = defaultdict(int)
8+
self.d = defaultdict(list)
9+
10+
def addPacket(self, source: int, destination: int, timestamp: int) -> bool:
11+
x = self.f(source, destination, timestamp)
12+
if x in self.vis:
13+
return False
14+
self.vis.add(x)
15+
if len(self.q) >= self.lim:
16+
self.forwardPacket()
17+
self.q.append((source, destination, timestamp))
18+
self.d[destination].append(timestamp)
19+
return True
20+
21+
def forwardPacket(self) -> List[int]:
22+
if not self.q:
23+
return []
24+
s, d, t = self.q.popleft()
25+
self.vis.remove(self.f(s, d, t))
26+
self.idx[d] += 1
27+
return [s, d, t]
28+
29+
def f(self, a: int, b: int, c: int) -> int:
30+
return a << 46 | b << 29 | c
31+
32+
def getCount(self, destination: int, startTime: int, endTime: int) -> int:
33+
ls = self.d[destination]
34+
k = self.idx[destination]
35+
i = bisect_left(ls, startTime, k)
36+
j = bisect_left(ls, endTime + 1, k)
37+
return j - i
38+
39+
40+
# Your Router object will be instantiated and called as such:
41+
# obj = Router(memoryLimit)
42+
# param_1 = obj.addPacket(source,destination,timestamp)
43+
# param_2 = obj.forwardPacket()
44+
# param_3 = obj.getCount(destination,startTime,endTime)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::collections::{HashSet, HashMap, VecDeque};
2+
3+
struct Router {
4+
lim: usize,
5+
vis: HashSet<i64>,
6+
q: VecDeque<(i32, i32, i32)>,
7+
idx: HashMap<i32, usize>,
8+
d: HashMap<i32, Vec<i32>>,
9+
}
10+
11+
impl Router {
12+
fn new(memory_limit: i32) -> Self {
13+
Router {
14+
lim: memory_limit as usize,
15+
vis: HashSet::new(),
16+
q: VecDeque::new(),
17+
idx: HashMap::new(),
18+
d: HashMap::new(),
19+
}
20+
}
21+
22+
fn f(a: i32, b: i32, c: i32) -> i64 {
23+
((a as i64) << 46) | ((b as i64) << 29) | (c as i64)
24+
}
25+
26+
fn add_packet(&mut self, source: i32, destination: i32, timestamp: i32) -> bool {
27+
let x = Self::f(source, destination, timestamp);
28+
if self.vis.contains(&x) {
29+
return false;
30+
}
31+
self.vis.insert(x);
32+
if self.q.len() >= self.lim {
33+
self.forward_packet();
34+
}
35+
self.q.push_back((source, destination, timestamp));
36+
self.d.entry(destination).or_default().push(timestamp);
37+
true
38+
}
39+
40+
fn forward_packet(&mut self) -> Vec<i32> {
41+
if let Some((s, d, t)) = self.q.pop_front() {
42+
self.vis.remove(&Self::f(s, d, t));
43+
*self.idx.entry(d).or_insert(0) += 1;
44+
vec![s, d, t]
45+
} else {
46+
vec![]
47+
}
48+
}
49+
50+
fn get_count(&self, destination: i32, start_time: i32, end_time: i32) -> i32 {
51+
let ls = self.d.get(&destination);
52+
if ls.is_none() {
53+
return 0;
54+
}
55+
let ls = ls.unwrap();
56+
let k = *self.idx.get(&destination).unwrap_or(&0);
57+
let i = k + ls[k..].partition_point(|&x| x < start_time);
58+
let j = k + ls[k..].partition_point(|&x| x < end_time + 1);
59+
(j - i) as i32
60+
}
61+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
class Router {
2+
private lim: number;
3+
private vis: Set<number>;
4+
private q: [number, number, number][];
5+
private idx: Map<number, number>;
6+
private d: Map<number, number[]>;
7+
8+
constructor(memoryLimit: number) {
9+
this.lim = memoryLimit;
10+
this.vis = new Set();
11+
this.q = [];
12+
this.idx = new Map();
13+
this.d = new Map();
14+
}
15+
16+
private f(a: number, b: number, c: number): number {
17+
return ((BigInt(a) << 46n) | (BigInt(b) << 29n) | BigInt(c)) as unknown as number;
18+
}
19+
20+
addPacket(source: number, destination: number, timestamp: number): boolean {
21+
const x = this.f(source, destination, timestamp);
22+
if (this.vis.has(x)) {
23+
return false;
24+
}
25+
this.vis.add(x);
26+
if (this.q.length >= this.lim) {
27+
this.forwardPacket();
28+
}
29+
this.q.push([source, destination, timestamp]);
30+
if (!this.d.has(destination)) {
31+
this.d.set(destination, []);
32+
}
33+
this.d.get(destination)!.push(timestamp);
34+
return true;
35+
}
36+
37+
forwardPacket(): number[] {
38+
if (this.q.length === 0) {
39+
return [];
40+
}
41+
const [s, d, t] = this.q.shift()!;
42+
this.vis.delete(this.f(s, d, t));
43+
this.idx.set(d, (this.idx.get(d) ?? 0) + 1);
44+
return [s, d, t];
45+
}
46+
47+
getCount(destination: number, startTime: number, endTime: number): number {
48+
const ls = this.d.get(destination) ?? [];
49+
const k = this.idx.get(destination) ?? 0;
50+
const i = this.lowerBound(ls, startTime, k);
51+
const j = this.lowerBound(ls, endTime + 1, k);
52+
return j - i;
53+
}
54+
55+
private lowerBound(arr: number[], target: number, from: number): number {
56+
let l = from,
57+
r = arr.length;
58+
while (l < r) {
59+
const m = Math.floor((l + r) / 2);
60+
if (arr[m] < target) {
61+
l = m + 1;
62+
} else {
63+
r = m;
64+
}
65+
}
66+
return l;
67+
}
68+
}
69+
70+
/**
71+
* Your Router object will be instantiated and called as such:
72+
* var obj = new Router(memoryLimit)
73+
* var param_1 = obj.addPacket(source,destination,timestamp)
74+
* var param_2 = obj.forwardPacket()
75+
* var param_3 = obj.getCount(destination,startTime,endTime)
76+
*/

0 commit comments

Comments
 (0)