-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path933.java
More file actions
76 lines (58 loc) · 1.95 KB
/
933.java
File metadata and controls
76 lines (58 loc) · 1.95 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
__________________________________________________________________________________________________
sample 57 ms submission
class RecentCounter {
private static final int MAX_INTERVAL = 3000;
private static final int MAX_BUFFER_SIZE = MAX_INTERVAL + 2;
private int[] buffer;
private int bufferStart;
private int bufferEnd;
public RecentCounter() {
buffer = new int[MAX_BUFFER_SIZE];
bufferStart = bufferEnd = 0;
}
public int ping(int t) {
buffer[bufferEnd++] = t;
bufferEnd %= (MAX_BUFFER_SIZE);
while (buffer[bufferStart] < t - MAX_INTERVAL) bufferStart = (bufferStart + 1) % MAX_BUFFER_SIZE;
if (bufferEnd > bufferStart) return bufferEnd - bufferStart;
return MAX_BUFFER_SIZE - (bufferStart - bufferEnd);
}
}
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/
__________________________________________________________________________________________________
sample 61408 kb submission
class RecentCounter {
private int[] history = new int[10_001];
private int start = -1;
private int end = -1;
public RecentCounter() {
}
public int ping(int t) {
//System.out.println(t);
if (start == -1) {
start = 0;
end = 0;
history[start] = t;
return 1;
}
end++;
if (end >= 10_001) {
end -= 10_001;
}
//System.out.println("end " + end);
history[end] = t;
while (t - history[start] > 3000) {
start++;
if (start >= 10_001) {
start -= 10_001;
}
}
//System.out.println("start " + start);
return end - start + 1;
}
}
__________________________________________________________________________________________________