Skip to content

Commit 54c12c8

Browse files
committed
Add solution #359
1 parent d891f69 commit 54c12c8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@
347347
356|[Line Reflection](./solutions/0356-line-reflection.js)|Medium|
348348
357|[Count Numbers with Unique Digits](./solutions/0357-count-numbers-with-unique-digits.js)|Medium|
349349
358|[Rearrange String k Distance Apart](./solutions/0358-rearrange-string-k-distance-apart.js)|Hard|
350+
359|[Logger Rate Limiter](./solutions/0359-logger-rate-limiter.js)|Easy|
350351
363|[Max Sum of Rectangle No Larger Than K](./solutions/0363-max-sum-of-rectangle-no-larger-than-k.js)|Hard|
351352
365|[Water and Jug Problem](./solutions/0365-water-and-jug-problem.js)|Medium|
352353
367|[Valid Perfect Square](./solutions/0367-valid-perfect-square.js)|Easy|

solutions/0359-logger-rate-limiter.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 359. Logger Rate Limiter
3+
* https://leetcode.com/problems/logger-rate-limiter/
4+
* Difficulty: Easy
5+
*
6+
* Design a logger system that receives a stream of messages along with their timestamps.
7+
* Each unique message should only be printed at most every 10 seconds (i.e. a message
8+
* printed at timestamp t will prevent other identical messages from being printed until
9+
* timestamp t + 10).
10+
*
11+
* All messages will come in chronological order. Several messages may arrive at the same timestamp.
12+
*
13+
* Implement the Logger class:
14+
* - Logger() Initializes the logger object.
15+
* - bool shouldPrintMessage(int timestamp, string message) Returns true if the message should
16+
* be printed in the given timestamp, otherwise returns false.
17+
*/
18+
19+
var Logger = function() {
20+
this.messageTimestamps = new Map();
21+
};
22+
23+
/**
24+
* @param {number} timestamp
25+
* @param {string} message
26+
* @return {boolean}
27+
*/
28+
Logger.prototype.shouldPrintMessage = function(timestamp, message) {
29+
const nextAllowed = this.messageTimestamps.get(message) || 0;
30+
if (timestamp >= nextAllowed) {
31+
this.messageTimestamps.set(message, timestamp + 10);
32+
return true;
33+
}
34+
return false;
35+
};

0 commit comments

Comments
 (0)