|
4 | 4 | import java.util.HashSet;
|
5 | 5 | import java.util.Map;
|
6 | 6 | import java.util.Set;
|
7 |
| -/**Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds. |
| 7 | +/** |
| 8 | + * 359. Logger Rate Limiter |
| 9 | + * |
| 10 | + * Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds. |
8 | 11 |
|
9 | 12 | Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.
|
10 | 13 |
|
@@ -33,33 +36,36 @@ Given a message and a timestamp (in seconds granularity), return true if the mes
|
33 | 36 | logger.shouldPrintMessage(11,"foo"); returns true;*/
|
34 | 37 | public class _359 {
|
35 | 38 |
|
36 |
| - class Logger { |
| 39 | + public static class Solution1 { |
| 40 | + class Logger { |
37 | 41 |
|
38 |
| - private Map<String, Integer> map; |
39 |
| - private Set<String> set; |
| 42 | + private Map<String, Integer> map; |
| 43 | + private Set<String> set; |
40 | 44 |
|
41 |
| - /** |
42 |
| - * Initialize your data structure here. |
43 |
| - */ |
44 |
| - public Logger() { |
45 |
| - map = new HashMap<String, Integer>(); |
46 |
| - set = new HashSet<String>(); |
47 |
| - } |
| 45 | + /** |
| 46 | + * Initialize your data structure here. |
| 47 | + */ |
| 48 | + public Logger() { |
| 49 | + map = new HashMap<String, Integer>(); |
| 50 | + set = new HashSet<String>(); |
| 51 | + } |
48 | 52 |
|
49 |
| - /** |
50 |
| - * Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. |
51 |
| - */ |
52 |
| - public boolean shouldPrintMessage(int timestamp, String message) { |
53 |
| - if (!set.contains(message)) { |
54 |
| - map.put(message, timestamp); |
55 |
| - set.add(message); |
56 |
| - return true; |
57 |
| - } else { |
58 |
| - if (timestamp - map.get(message) < 10) { |
59 |
| - return false; |
60 |
| - } else { |
| 53 | + /** |
| 54 | + * Returns true if the message should be printed in the given timestamp, otherwise returns |
| 55 | + * false. The timestamp is in seconds granularity. |
| 56 | + */ |
| 57 | + public boolean shouldPrintMessage(int timestamp, String message) { |
| 58 | + if (!set.contains(message)) { |
61 | 59 | map.put(message, timestamp);
|
| 60 | + set.add(message); |
62 | 61 | return true;
|
| 62 | + } else { |
| 63 | + if (timestamp - map.get(message) < 10) { |
| 64 | + return false; |
| 65 | + } else { |
| 66 | + map.put(message, timestamp); |
| 67 | + return true; |
| 68 | + } |
63 | 69 | }
|
64 | 70 | }
|
65 | 71 | }
|
|
0 commit comments