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;
struct Solution;
impl Solution {
pub fn sort_vowels(s: String) -> String {
let mut chars = vec![0;10];
let mut indexs = vec![];
let mut s : Vec<char> = s.chars().collect();
for i in 0..s.len() {
let c = s[i];
let idx = "AEIOUaeiou".find(c);
if let Some(idx) = idx {
chars[idx] += 1;
indexs.push(i);
#[derive(Hash, Eq, PartialEq, Clone)]
struct Node {
source: i32,
destination: i32,
timestamp: i32
}
struct MpNode {
nodes: Vec<i32>,
start: usize
}
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);
}
}
let mut i = 0;
for j in 0..10 {
let c = "AEIOUaeiou".chars().nth(j).unwrap();
while chars[j] > 0 {
chars[j] -= 1;
s[indexs[i]] = c;
i += 1;
if self.arr.len() - self.start > self.limit as usize {
let remove_node = &self.arr[self.start];
self.set.remove(remove_node);
let destination = remove_node.destination;
self.start += 1;
if let Some(mp_node) = self.mp.get_mut(&destination) {
mp_node.start += 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() {
let sl = Solution::sort_vowels("lEetcOde".to_string());
println!("{:?}", sl);
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![]];
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);
}
}
}