3508. 设计路由器

This commit is contained in:
li-chx 2025-09-26 16:39:15 +08:00
parent 4692c200d8
commit 74255bbe8b
1 changed files with 134 additions and 23 deletions

View File

@ -1,32 +1,143 @@
use std::cmp::max;
use std::collections::{HashMap, HashSet};
mod arr; mod arr;
struct Solution;
impl Solution { #[derive(Hash, Eq, PartialEq, Clone)]
pub fn sort_vowels(s: String) -> String { struct Node {
let mut chars = vec![0;10]; source: i32,
let mut indexs = vec![]; destination: i32,
let mut s : Vec<char> = s.chars().collect(); timestamp: i32
for i in 0..s.len() { }
let c = s[i]; struct MpNode {
let idx = "AEIOUaeiou".find(c); nodes: Vec<i32>,
if let Some(idx) = idx { start: usize
chars[idx] += 1; }
indexs.push(i); struct Router {
set : HashSet<Node>,
arr : Vec<Node>,
start: usize,
limit: i32,
mp : HashMap<i32, MpNode>
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
// source 开始 destination 结束 timestamp 时间戳
// memoryLimit 内存限制
impl Router {
fn new(memory_limit: i32) -> Self {
let router = Router {
set: HashSet::new(),
arr: vec![],
start: 0,
limit: memory_limit,
mp: HashMap::new()
};
router
}
fn add_packet(&mut self, source: i32, destination: i32, timestamp: i32) -> bool {
let node = Node {
source,
destination,
timestamp
};
if self.set.contains(&node) {
false
} else {
self.set.insert(node.clone());
self.arr.push(node.clone());
if let Some(mp_node) = self.mp.get_mut(&destination) {
mp_node.nodes.push(timestamp);
}else {
let mp_node = MpNode {
nodes: vec![timestamp],
start: 0
};
self.mp.insert(destination, mp_node);
} }
} if self.arr.len() - self.start > self.limit as usize {
let mut i = 0; let remove_node = &self.arr[self.start];
for j in 0..10 { self.set.remove(remove_node);
let c = "AEIOUaeiou".chars().nth(j).unwrap(); let destination = remove_node.destination;
while chars[j] > 0 { self.start += 1;
chars[j] -= 1; if let Some(mp_node) = self.mp.get_mut(&destination) {
s[indexs[i]] = c; mp_node.start += 1;
i += 1; }else {
print!("error occur {source} {destination} {timestamp}");
}
} }
true
} }
s.into_iter().collect() }
fn forward_packet(&mut self) -> Vec<i32> {
if self.arr.len() == self.start {
return vec![];
}
let mut ans = vec![];
let node = &self.arr[self.start];
ans.push(node.source);
ans.push(node.destination);
ans.push(node.timestamp);
self.set.remove(&self.arr[self.start]);
self.start += 1;
if let Some(node_mp) = self.mp.get_mut(&node.destination) {
node_mp.start += 1;
}
ans
}
fn get_count(&self, destination: i32, start_time: i32, end_time: i32) -> i32 {
let mut mp_node = match self.mp.get(&destination) {
Some(v) => v,
None => return 0
};
let mut l: i32 = 0;
let mut r: i32 = 0;
match mp_node.nodes.binary_search(&(start_time - 1)) {
Ok(mut v) => {
while v < mp_node.nodes.len() && mp_node.nodes[v] == start_time - 1 {
v+=1;
}
l = v as i32;
}
Err(v) => l = v as i32
}
match mp_node.nodes.binary_search(&(end_time+1)) {
Ok(v) => {
let mut v = v as i32;
while v >= 0 && mp_node.nodes[v as usize] == end_time + 1 {
v-=1;
}
r = v + 1;
}
Err(v) => r = v as i32
}
l = max(l , mp_node.start as i32);
max(r - l, 0)
} }
} }
fn main() { fn main() {
let sl = Solution::sort_vowels("lEetcOde".to_string()); let data = vec![vec![4], vec![2,3,1], vec![5,3,2],vec![3,1,1],vec![3,1,1],vec![2,4,2],vec![4,3,2],vec![]];
println!("{:?}", sl); let methods = vec!["Router","addPacket","addPacket","getCount","getCount","addPacket","addPacket","forwardPacket"];
let mut obj = Router::new(data[0][0]);
for i in 1..methods.len() {
if methods[i] == "addPacket" {
let ret: bool = obj.add_packet(data[i][0], data[i][1], data[i][2]);
println!("{:?}", ret);
} else if methods[i] == "forwardPacket" {
let ret: Vec<i32> = obj.forward_packet();
println!("{:?}", ret);
} else if methods[i] == "getCount" {
let ret: i32 = obj.get_count(data[i][0], data[i][1], data[i][2]);
println!("{:?}", ret);
}
}
} }