@@ -46,21 +46,103 @@ logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, nex
4646 <li>At most <code>10<sup>4</sup></code> calls will be made to <code>shouldPrintMessage</code>.</li>
4747</ul >
4848
49-
5049## Solutions
5150
5251<!-- tabs:start -->
5352
5453### ** Python3**
5554
5655``` python
57-
56+ class Logger :
57+
58+ def __init__ (self ):
59+ """
60+ Initialize your data structure here.
61+ """
62+ self .limiter = {}
63+
64+ def shouldPrintMessage (self , timestamp : int , message : str ) -> bool :
65+ """
66+ Returns true if the message should be printed in the given timestamp, otherwise returns false.
67+ If this method returns false, the message will not be printed.
68+ The timestamp is in seconds granularity.
69+ """
70+ t = self .limiter.get(message, 0 )
71+ if t > timestamp:
72+ return False
73+ self .limiter[message] = timestamp + 10
74+ return True
75+
76+
77+ # Your Logger object will be instantiated and called as such:
78+ # obj = Logger()
79+ # param_1 = obj.shouldPrintMessage(timestamp,message)
5880```
5981
6082### ** Java**
6183
6284``` java
85+ class Logger {
86+
87+ private Map<String , Integer > limiter;
88+
89+ /* * Initialize your data structure here. */
90+ public Logger () {
91+ limiter = new HashMap<> ();
92+ }
93+
94+ /* * Returns true if the message should be printed in the given timestamp, otherwise returns false.
95+ If this method returns false, the message will not be printed.
96+ The timestamp is in seconds granularity. */
97+ public boolean shouldPrintMessage (int timestamp , String message ) {
98+ int t = limiter. getOrDefault(message, 0 );
99+ if (t > timestamp) {
100+ return false ;
101+ }
102+ limiter. put(message, timestamp + 10 );
103+ return true ;
104+ }
105+ }
106+
107+ /**
108+ * Your Logger object will be instantiated and called as such:
109+ * Logger obj = new Logger();
110+ * boolean param_1 = obj.shouldPrintMessage(timestamp,message);
111+ */
112+ ```
63113
114+ ### ** JavaScript**
115+
116+ ``` js
117+ /**
118+ * Initialize your data structure here.
119+ */
120+ var Logger = function () {
121+ this .limiter = {};
122+ };
123+
124+ /**
125+ * Returns true if the message should be printed in the given timestamp, otherwise returns false.
126+ If this method returns false, the message will not be printed.
127+ The timestamp is in seconds granularity.
128+ * @param {number} timestamp
129+ * @param {string} message
130+ * @return {boolean}
131+ */
132+ Logger .prototype .shouldPrintMessage = function (timestamp , message ) {
133+ const t = this .limiter [message] || 0 ;
134+ if (t > timestamp) {
135+ return false ;
136+ }
137+ this .limiter [message] = timestamp + 10 ;
138+ return true ;
139+ };
140+
141+ /**
142+ * Your Logger object will be instantiated and called as such:
143+ * var obj = new Logger()
144+ * var param_1 = obj.shouldPrintMessage(timestamp,message)
145+ */
64146```
65147
66148### ** ...**
0 commit comments