Skip to content

Commit d47512a

Browse files
committed
188th Commit
1 parent 4224cba commit d47512a

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ Ace Coding Interview with 75 Qs
149149
[735]: ./src/page-7/735.%20Asteroid%20Collision/asteroidCollision.ts
150150
[394]: ./src/page-4/394.%20Decode%20String/decodeString.ts
151151

152-
| Queue | | |
153-
| --------------------------- | -------- | ------ |
154-
| 933. Number of Recent Calls | Solution | Easy |
155-
| 649. Dota2 Senate | Solution | Medium |
152+
| Queue | | |
153+
| --------------------------- | --------------- | ------ |
154+
| 933. Number of Recent Calls | [Solution][933] | Easy |
155+
| 649. Dota2 Senate | Solution | Medium |
156+
157+
[933]: ./src/page-9/933.%20Number%20of%20Recent%20Calls/RecentCounter.ts
156158

157159
| Linked List | | |
158160
| --------------------------------------------- | --------------- | ------ |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { RecentCounter } from './RecentCounter';
2+
3+
describe('933. Number of Recent Calls', () => {
4+
test('RecentCounter', () => {
5+
const recentCounter = new RecentCounter();
6+
expect(recentCounter.ping(1)).toBe(1);
7+
expect(recentCounter.ping(100)).toBe(2);
8+
expect(recentCounter.ping(3001)).toBe(3);
9+
expect(recentCounter.ping(3002)).toBe(3);
10+
});
11+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Accepted
3+
*/
4+
export class RecentCounter {
5+
private queue: number[];
6+
7+
constructor() {
8+
this.queue = [];
9+
}
10+
11+
ping(t: number): number {
12+
// Add the new request to the queue
13+
this.queue.push(t);
14+
15+
// Remove requests that are outside the 3000 milliseconds window
16+
while (this.queue[0] < t - 3000) {
17+
this.queue.shift();
18+
}
19+
20+
// The size of the queue is the number of requests in the last 3000 milliseconds
21+
return this.queue.length;
22+
}
23+
}

0 commit comments

Comments
 (0)