Dashboard Temp Share Shortlinks Frames API

HTMLify

police-and-thieves.py
Views: 2 | Author: prakhardoneria
 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
class Solution:
    def catchThieves(self, arr, k):
        n = len(arr)
        p = 0
        t = 0
        res = 0
        
        police = []
        thieves = []
        
        for i in range(n):
            if arr[i] == 'P':
                police.append(i)
            else:
                thieves.append(i)
                
        while p < len(police) and t < len(thieves):
            if abs(police[p] - thieves[t]) <= k:
                res += 1
                p += 1
                t += 1
            elif police[p] < thieves[t]:
                p += 1
            else:
                t += 1
                
        return res