
- 생성자에서 Queue를 선언해준다.
- ping메소드에서 q에 t를 담아준다.
- while을 통해 Queue안에 있는 값들이 t-3000보다 작은 경우 remove();
- Queue size()를 리턴한다.
class RecentCounter {
Queue<Integer> q;
public RecentCounter() {
q = new LinkedList();
}
public int ping(int t) {
q.add(t);
while(q.peek() < t- 3000){
q.remove();
}
return q.size();
}
}
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/
Queue에 모든 requests의 값을 담은 후 FIFO를 통해 해당 범위안에 있는지 체크하여 문제를 해결했다.
- 오늘의 문제가 Queue란걸 알고 풀어서 쉽게 접근할 수 있었던것 같다.
- 스택과 큐에대한 문제를 많이 풀어봐야할것 같다.
- 문제를 이해하기에 설명이 조금 이상한것 같다.
- 스택과 큐
- 알고리즘 공부