-
Notifications
You must be signed in to change notification settings - Fork 2
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
4224cba
commit d47512a
Showing
3 changed files
with
40 additions
and
4 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
11 changes: 11 additions & 0 deletions
11
src/page-9/933. Number of Recent Calls/RecentCounter.test.ts
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,11 @@ | ||
import { RecentCounter } from './RecentCounter'; | ||
|
||
describe('933. Number of Recent Calls', () => { | ||
test('RecentCounter', () => { | ||
const recentCounter = new RecentCounter(); | ||
expect(recentCounter.ping(1)).toBe(1); | ||
expect(recentCounter.ping(100)).toBe(2); | ||
expect(recentCounter.ping(3001)).toBe(3); | ||
expect(recentCounter.ping(3002)).toBe(3); | ||
}); | ||
}); |
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,23 @@ | ||
/** | ||
* Accepted | ||
*/ | ||
export class RecentCounter { | ||
private queue: number[]; | ||
|
||
constructor() { | ||
this.queue = []; | ||
} | ||
|
||
ping(t: number): number { | ||
// Add the new request to the queue | ||
this.queue.push(t); | ||
|
||
// Remove requests that are outside the 3000 milliseconds window | ||
while (this.queue[0] < t - 3000) { | ||
this.queue.shift(); | ||
} | ||
|
||
// The size of the queue is the number of requests in the last 3000 milliseconds | ||
return this.queue.length; | ||
} | ||
} |