-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContanimation.java
More file actions
62 lines (51 loc) · 1.78 KB
/
Contanimation.java
File metadata and controls
62 lines (51 loc) · 1.78 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
/*
* https://www.careercup.com/question?id=5078647835197440
*/
class Contanimation {
public static void findInfected(int[] person, int d) {
if (person == null || d < 0) {
throw new IllegalArgumentException();
}
int maxInfected = Integer.MIN_VALUE;
int minInfected = Integer.MAX_VALUE;
int left = 0;
int right = 0;
int n = person.length;
for (int i = 0; i < n; ++i) {
while (right < n && (person[right] - person[i] < d)) {
right++;
}
if (right >= n || (person[right] - person[i] > d)) {
right--;
}
while (left < i && (person[i] - person[left] > d)) {
left++;
}
if (left > i) {
left--;
}
// Exclude the spreader person
int currentInfections = (right - left - 1) + 1;
if (maxInfected < currentInfections || minInfected > currentInfections) {
System.out.println(left + " : " + right + ", infected = " + (currentInfections) + ", spreader : " + i);
}
maxInfected = Math.max(maxInfected, currentInfections);
minInfected = Math.min(minInfected, currentInfections);
}
if (maxInfected == Integer.MIN_VALUE) {
maxInfected = 0;
}
if (minInfected == Integer.MAX_VALUE) {
minInfected = 0;
}
System.out.println("Max infected person : " + maxInfected);
System.out.println("Min infected person : " + minInfected);
}
}
class Test {
public static void main(String[] args) {
int d = 5;
int[] person = {1, 3, 5, 9, 14, 15, 16, 17, 18, 19, 20};
Contanimation.findInfected(person, d);
}
}