From 3b147a27d9beb0cd539a66c9945f7beea2e342ac Mon Sep 17 00:00:00 2001 From: rsimpson2 Date: Thu, 22 Jan 2026 09:37:20 -0500 Subject: [PATCH] LeetCode Rate Limiter Challenge --- 0359-logger-rate-limiter/README.md | 45 ----------------- .../easy/0359-logger-rate-limiter/README.md | 50 +++++++++++++++++++ .../logger-rate-limiter.ts | 4 +- 3 files changed, 52 insertions(+), 47 deletions(-) delete mode 100644 0359-logger-rate-limiter/README.md create mode 100644 leetcode/easy/0359-logger-rate-limiter/README.md rename {0359-logger-rate-limiter => leetcode/easy/0359-logger-rate-limiter}/logger-rate-limiter.ts (82%) diff --git a/0359-logger-rate-limiter/README.md b/0359-logger-rate-limiter/README.md deleted file mode 100644 index c45919b..0000000 --- a/0359-logger-rate-limiter/README.md +++ /dev/null @@ -1,45 +0,0 @@ - -# [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter) ![](https://img.shields.io/badge/Easy-green) - -

Design a logger system that receives a stream of messages along with their timestamps. Each unique message should only be printed at most every 10 seconds (i.e. a message printed at timestamp t will prevent other identical messages from being printed until timestamp t + 10).

- -

All messages will come in chronological order. Several messages may arrive at the same timestamp.

- -

Implement the Logger class:

- - - -

 

-

Example 1:

- -
-Input
-["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]
-[[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"], [11, "foo"]]
-Output
-[null, true, true, false, false, false, true]
-
-Explanation
-Logger logger = new Logger();
-logger.shouldPrintMessage(1, "foo");  // return true, next allowed timestamp for "foo" is 1 + 10 = 11
-logger.shouldPrintMessage(2, "bar");  // return true, next allowed timestamp for "bar" is 2 + 10 = 12
-logger.shouldPrintMessage(3, "foo");  // 3 < 11, return false
-logger.shouldPrintMessage(8, "bar");  // 8 < 12, return false
-logger.shouldPrintMessage(10, "foo"); // 10 < 11, return false
-logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, next allowed timestamp for "foo" is 11 + 10 = 21
-
- -

 

-

Constraints:

- - - - \ No newline at end of file diff --git a/leetcode/easy/0359-logger-rate-limiter/README.md b/leetcode/easy/0359-logger-rate-limiter/README.md new file mode 100644 index 0000000..53bd00f --- /dev/null +++ b/leetcode/easy/0359-logger-rate-limiter/README.md @@ -0,0 +1,50 @@ +# [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter) ![Shield](https://img.shields.io/badge/Easy-green) + +Design a logger system that receives a stream of messages along with their timestamps. Each **unique** message should only be printed **at most every 10 seconds** (i.e. a message printed at timestamp `t` will prevent other identical messages from being printed until timestamp `t + 10`). + +All messages will come in chronological order. Several messages may arrive at the same timestamp. + +## Implementation + +Implement the `Logger` class: + +- `Logger()`: Initializes the `logger` object. +- `bool shouldPrintMessage(int timestamp, string message)`: Returns `true` if the `message` should be printed at the given `timestamp`, otherwise returns `false`. + +--- + +### Example + +#### Input + +```bash +["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"] +[[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"], [11, "foo"]] +``` + +#### Output + +```bash +[null, true, true, false, false, false, true] +``` + +#### Explanation + +```bash +Logger logger = new Logger(); +logger.shouldPrintMessage(1, "foo"); // return true, next allowed timestamp for "foo" is 1 + 10 = 11 +logger.shouldPrintMessage(2, "bar"); // return true, next allowed timestamp for "bar" is 2 + 10 = 12 +logger.shouldPrintMessage(3, "foo"); // 3 < 11, return false +logger.shouldPrintMessage(8, "bar"); // 8 < 12, return false +logger.shouldPrintMessage(10, "foo"); // 10 < 11, return false +logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, next allowed timestamp for "foo" is 11 + 10 = 21 +``` + +--- + +## Constraints + +- `0 <= timestamp <= 10^9` +- Every `timestamp` will be passed in non-decreasing order (chronological order). +- `1 <= message.length <= 30` +- At most `10^4` calls will be made to `shouldPrintMessage`. diff --git a/0359-logger-rate-limiter/logger-rate-limiter.ts b/leetcode/easy/0359-logger-rate-limiter/logger-rate-limiter.ts similarity index 82% rename from 0359-logger-rate-limiter/logger-rate-limiter.ts rename to leetcode/easy/0359-logger-rate-limiter/logger-rate-limiter.ts index f7536d6..3bbcfa3 100644 --- a/0359-logger-rate-limiter/logger-rate-limiter.ts +++ b/leetcode/easy/0359-logger-rate-limiter/logger-rate-limiter.ts @@ -1,9 +1,9 @@ -class Logger { +export class Logger { messageMap = new Map(); shouldPrintMessage(timestamp: number, message: string): boolean { if (this.messageMap.has(message)) { - const lastTimestamp = this.messageMap.get(message); + const lastTimestamp = this.messageMap.get(message)!; if (timestamp >= lastTimestamp + 10) { this.messageMap.set(message, timestamp); return true;