-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_362.java
More file actions
56 lines (48 loc) · 1.85 KB
/
_362.java
File metadata and controls
56 lines (48 loc) · 1.85 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
package leetcode;
import java.util.*;
class HitCounter {
private final Stack<Integer> stack = new Stack<>();
public void hit(int timestamp) {
stack.push(timestamp);
}
public int getHits(int timestamp) {
int sum = 0;
Stack<Integer> s = (Stack<Integer>) stack.clone();
while (!s.isEmpty() && s.pop() > timestamp - 300) {
sum++;
}
return sum;
}
// private final Map<Integer, Integer> map = new HashMap<>();
//
// public void hit(int timestamp) {
// map.put(timestamp, map.getOrDefault(timestamp, 0) + 1);
// }
//
// public int getHits(int timestamp) {
// return map.entrySet().stream()
// .filter(x -> x.getKey() > timestamp - 300 && x.getKey() <= timestamp)
// .map(Map.Entry::getValue)
// .mapToInt(Integer::intValue)
// .sum();
// }
}
public class _362 {
public static void main(String[] args) {
HitCounter counter = new HitCounter();
System.out.println(counter.getHits(1));
counter.hit(1); // timestamp 1에 hit 기록
counter.hit(2); // timestamp 2에 hit 기록
counter.hit(3); // timestamp 3에 hit 기록
System.out.println(counter.getHits(3)); // timestamp 4로부터 최근 5분간의 hit수인 3을 return
System.out.println(counter.getHits(4)); // timestamp 4로부터 최근 5분간의 hit수인 3을 return
counter.hit(300); // timestamp 300에 hit 기록
System.out.println(counter.getHits(300)); // timestamp 300으로부터 최근 5분간의 hit수인 4를 리턴
System.out.println(counter.getHits(301)); // timestamp 301로부터 최근 5분간의 hit 수인 3을 리턴
//
// for (int i = 0; i < 5000; i++) {
// counter.hit(i);
// }
// System.out.println(counter.getHits(4500));
}
}