Created
February 15, 2024 08:45
-
-
Save ritik-agrawal/d94cccab4a7d938f8082e57588dc2f40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RecentCounter { | |
List<Integer> queue; | |
int head; | |
int tail; | |
public RecentCounter() { | |
this.queue = new ArrayList<Integer>(); | |
this.head = -1; | |
this.tail = -1; | |
} | |
public int ping(int t) { | |
add(t); | |
remove(t); | |
return (tail - head) + 1; | |
} | |
private void add(int t){ | |
if (queue.size() == 0){ | |
head++; | |
} | |
queue.add(t); | |
tail++; | |
} | |
private void remove(int t){ | |
var low = t - 3000; | |
while ( | |
(head <= tail) && | |
(low >= 0) && | |
(queue.get(head) < low) | |
){ | |
head++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leetcode
Link: https://leetcode.com/problems/number-of-recent-calls/description/?envType=study-plan-v2&envId=leetcode-75
Achievement