id 1414830744 Time: 18 ms MemUsage: 6.3 MB

This commit is contained in:
2024-10-22 10:19:44 +09:00
parent d24e9e9740
commit dd6dc8f70a

View File

@@ -0,0 +1,36 @@
use std::collections::VecDeque;
struct RecentCounter {
times: VecDeque<i32>
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl RecentCounter {
fn new() -> Self {
Self{
times: VecDeque::new()
}
}
fn ping(&mut self, t: i32) -> i32 {
while let Some(x) = self.times.pop_front(){
if t - x <= 3000 {
self.times.push_front(x);
break;
}
}
self.times.push_back(t);
self.times.len() as i32
}
}
/**
* Your RecentCounter object will be instantiated and called as such:
* let obj = RecentCounter::new();
* let ret_1: i32 = obj.ping(t);
*/