File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 347
347
356|[ Line Reflection] ( ./solutions/0356-line-reflection.js ) |Medium|
348
348
357|[ Count Numbers with Unique Digits] ( ./solutions/0357-count-numbers-with-unique-digits.js ) |Medium|
349
349
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|
350
351
363|[ Max Sum of Rectangle No Larger Than K] ( ./solutions/0363-max-sum-of-rectangle-no-larger-than-k.js ) |Hard|
351
352
365|[ Water and Jug Problem] ( ./solutions/0365-water-and-jug-problem.js ) |Medium|
352
353
367|[ Valid Perfect Square] ( ./solutions/0367-valid-perfect-square.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments