-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 110.java
More file actions
33 lines (28 loc) · 803 Bytes
/
Day 110.java
File metadata and controls
33 lines (28 loc) · 803 Bytes
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
class Solution {
public int catchThieves(char[] arr, int k) {
ArrayList<Integer> police = new ArrayList<>();
ArrayList<Integer> thieves = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 'P')
police.add(i);
else if (arr[i] == 'T')
thieves.add(i);
}
int i = 0, j = 0;
int count = 0;
while (i < police.size() && j < thieves.size()) {
int p = police.get(i);
int t = thieves.get(j);
if (Math.abs(p - t) <= k) {
count++;
i++;
j++;
} else if (t < p) {
j++;
} else {
i++;
}
}
return count;
}
}