-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentCounter933.java
More file actions
60 lines (51 loc) · 1.39 KB
/
RecentCounter933.java
File metadata and controls
60 lines (51 loc) · 1.39 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
import java.util.*;
public class RecentCounter933 {
public final LinkedList<Integer> list;
public RecentCounter933() {
list = new LinkedList<>();
}
public int ping(int t) {
int ret = 0;
list.add(t);
System.out.println("\n-ping: " + t + " \t" + list);
List<Integer> removeList = new ArrayList<>();
for(Integer each : list) {
int diff = t - each ;
System.out.println("\t" + each + " (" + t + " - " + each + ") = " + (diff) + " ... " + (diff <= 3000));
if(diff <= 3000) {
ret++;
}
else {
removeList.add(each);
}
}
for(Integer each : removeList) {
list.remove(each);
}
return ret;
}
public static void main(String[] args) {
RecentCounter933 obj = new RecentCounter933();
System.out.println("ping 1 " + obj.ping(1));
System.out.println("ping 100 " + obj.ping(100));
System.out.println("ping 3001 " + obj.ping(3001));
System.out.println("ping 3002 " + obj.ping(3002));
System.out.println("ping 3003 " + obj.ping(3003));
/*
int count = 0;
Random rand = new Random();
int time = 4;
do {
time += rand.nextInt(50);
try {
Thread.sleep(rand.nextInt(200));
}
catch(Exception e) {
e.printStackTrace();
}
obj.ping(time);
count++;
} while(count < 6);
*/
}
}