Skip to content

Commit cbba375

Browse files
logger rate limiter
1 parent 83fde9e commit cbba375

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

‎EASY/src/easy/LoggerRateLimiter.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package easy;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
public class LoggerRateLimiter {
9+
10+
}
11+
12+
class Logger {
13+
14+
private Map<String, Integer> map;
15+
private Set<String> set;
16+
17+
/** Initialize your data structure here. */
18+
public Logger() {
19+
map = new HashMap<String, Integer>();
20+
set = new HashSet<String>();
21+
}
22+
23+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. */
24+
public boolean shouldPrintMessage(int timestamp, String message) {
25+
if(!set.contains(message)) {
26+
map.put(message, timestamp);
27+
set.add(message);
28+
return true;
29+
} else {
30+
if(timestamp - map.get(message) < 10) return false;
31+
else{
32+
map.put(message, timestamp);
33+
return true;
34+
}
35+
}
36+
}
37+
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
|379|[Design Phone Directory](https://leetcode.com/problems/design-phone-directory/)|[Solution](../../blob/master/MEDIUM/src/medium/DesignPhoneDirectory.java)| O(1)|O(n) | Medium|
3333
|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](../../blob/master/EASY/src/easy/GuessNumberHigherorLower.java)| O(logn)|O(1) | Easy| Binary Search
3434
|370|[Range Addition](https://leetcode.com/problems/range-addition/)|[Solution](../../blob/master/MEDIUM/src/medium/RangeAddition.java)| O(n+k)|O(1) | Medium|
35+
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../../blob/master/EASY/src/easy/LoggerRateLimiter.java)| amortized O(1)|O(k) | Easy| HashMap
3536
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
3637
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search
3738
|346|[Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)|[Solution](../../blob/master/EASY/src/easy/MovingAveragefromDataStream.java)| O(1)|O(w)) | Easy| Queue

0 commit comments

Comments
 (0)