
-Code
import java.util.*;
class RecentCounter {
static Deque<Integer> dq;
public RecentCounter() {
dq = new ArrayDeque<>();
}
public int ping(int t) {
dq.addLast(t);
// 범위보다 작은 값 poll
while(dq.peekFirst() < t - 3000) {
dq.pollFirst();
}
return dq.size();
}
}
문제 이해를 처음에 못해서 찾아보니 그냥 범위가 t - 3000에서 t까지 인데 그 안에 포함된 값들의 개수만 출력이었습니다. 그래서 큐를 이용해서 해결했고 추가적으로 RecentCounter 호출시 초기화를 해줘야 한다고 해서 RecentCounter에 초기화시켜주면 끝이었습니다.
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [LeetCode/Java] Leaf-Similar Trees (0) | 2025.12.31 |
|---|---|
| [LeetCode/Java] Maximum Depth of Binary Tree (0) | 2025.12.31 |
| [LeetCode/Java] Unique Number of Occurrences (0) | 2025.12.31 |
| [LeetCode/Java] Find the Difference of Two Arrays (0) | 2025.12.31 |
| [LeetCode/Java] Find Pivot Index (0) | 2025.12.31 |