반응형
1. 문제
- 요구 사항에 맞는 RecenCounter class를 구현하라.
2. 해결
class RecentCounter {
queue: number[]
constructor() {
this.queue = [];
}
ping(t: number): number {
const min = t - 3000, max = t;
let recent = 0;
this.queue.push(t);
while(this.queue[0] < min || this.queue[0] > max) {
this.queue.shift();
}
return this.queue.length
}
}
/**
* Your RecentCounter object will be instantiated and called as such:
* var obj = new RecentCounter()
* var param_1 = obj.ping(t)
*/
- 입력받은 ping 시간을 큐에 넣고, 범위에 맞지 않는 값들은 큐에서 제거한다.