Skip to content

Commit

Permalink
188th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 4, 2024
1 parent 4224cba commit d47512a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ Ace Coding Interview with 75 Qs
[735]: ./src/page-7/735.%20Asteroid%20Collision/asteroidCollision.ts
[394]: ./src/page-4/394.%20Decode%20String/decodeString.ts

| Queue | | |
| --------------------------- | -------- | ------ |
| 933. Number of Recent Calls | Solution | Easy |
| 649. Dota2 Senate | Solution | Medium |
| Queue | | |
| --------------------------- | --------------- | ------ |
| 933. Number of Recent Calls | [Solution][933] | Easy |
| 649. Dota2 Senate | Solution | Medium |

[933]: ./src/page-9/933.%20Number%20of%20Recent%20Calls/RecentCounter.ts

| Linked List | | |
| --------------------------------------------- | --------------- | ------ |
Expand Down
11 changes: 11 additions & 0 deletions src/page-9/933. Number of Recent Calls/RecentCounter.test.ts
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);
});
});
23 changes: 23 additions & 0 deletions src/page-9/933. Number of Recent Calls/RecentCounter.ts
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;
}
}

0 comments on commit d47512a

Please sign in to comment.