-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_359.java
More file actions
107 lines (88 loc) · 2.94 KB
/
Copy pathP_359.java
File metadata and controls
107 lines (88 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package leetcode.easy;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("unused")
public class P_359 {
static class LoggerCHM {
ConcurrentHashMap<String, Integer> lastPrintTime;
LoggerCHM() {
lastPrintTime = new ConcurrentHashMap<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
final Integer last = lastPrintTime.get(message);
return last == null && lastPrintTime.put(message, timestamp) == null
|| last != null && timestamp - last >= 10 && lastPrintTime.replace(message, last, timestamp);
}
}
static class Logger {
Map<String, Integer> map;
Logger() {
map = new HashMap<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
if (map.containsKey(message)) {
if (map.get(message) <= timestamp - 10) {
map.put(message, timestamp);
return true;
}
return false;
}
map.put(message, timestamp);
return true;
}
}
static class LoggerQueueSet {
static class Entry {
int timestamp;
String msg;
Entry(int timestamp, String msg) {
this.timestamp = timestamp;
this.msg = msg;
}
}
Deque<Entry> pq;
Set<String> set;
LoggerQueueSet() {
pq = new LinkedList<>();
set = new HashSet<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
while (!pq.isEmpty() && timestamp - pq.peekLast().timestamp >= 10) {
set.remove(pq.removeLast().msg);
}
if (set.contains(message)) {
return false;
}
set.add(message);
pq.addFirst(new Entry(timestamp, message));
return true;
}
}
static class LoggerLHM {
public Map<String, Integer> map;
int lastSecond;
LoggerLHM() {
map = new LinkedHashMap<>() {
private static final long serialVersionUID = -3418750008137711818L;
@Override
protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
return lastSecond - eldest.getValue() >= 10;
}
};
}
public boolean shouldPrintMessage(int timestamp, String message) {
lastSecond = timestamp;
if (!map.containsKey(message) || timestamp - map.get(message) >= 10) {
map.put(message, timestamp);
return true;
}
return false;
}
}
}