-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b54f87c
commit 06e1245
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
933. Number of Recent Calls | ||
Solved | ||
Easy | ||
Topics | ||
Companies | ||
You have a RecentCounter class which counts the number of recent requests within a certain time frame. | ||
Implement the RecentCounter class: | ||
RecentCounter() Initializes the counter with zero recent requests. | ||
int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t]. | ||
It is guaranteed that every call to ping uses a strictly larger value of t than the previous call. | ||
*// | ||
|
||
class RecentCounter { | ||
private static final int[] records = new int[10000]; // | ||
private int start; | ||
private int end; | ||
|
||
public RecentCounter() { | ||
start = 0; | ||
end = 0; | ||
} | ||
|
||
public int ping(int t) { | ||
while (start < end && (t - records[start] > 3000)) { | ||
start++; // if the difference in time is greater than 3000ms, | ||
// than increase the value of start unitl it's equal or less than 3000ms. | ||
} | ||
records[end++] = t; // Inserting the current time at the end | ||
return end - start; // Returning the answer including the element added just now. | ||
} | ||
} |