Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80c5fcc

Browse files
Create Solution.py
1 parent 9cf62f9 commit 80c5fcc

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Python3 program to find maximum
2+
# number of thieves caught
3+
4+
# Returns maximum number of thieves
5+
# that can be caught.
6+
def policeThief(arr, n, k):
7+
i = 0
8+
l = 0
9+
r = 0
10+
res = 0
11+
thi = []
12+
pol = []
13+
14+
# store indices in list
15+
while i < n:
16+
if arr[i] == 'P':
17+
pol.append(i)
18+
elif arr[i] == 'T':
19+
thi.append(i)
20+
i += 1
21+
22+
# track lowest current indices of
23+
# thief: thi[l], police: pol[r]
24+
while l < len(thi) and r < len(pol):
25+
26+
# can be caught
27+
if (abs( thi[l] - pol[r] ) <= k):
28+
res += 1
29+
l += 1
30+
r += 1
31+
32+
# increment the minimum index
33+
elif thi[l] < pol[r]:
34+
l += 1
35+
else:
36+
r += 1
37+
38+
return res
39+
40+
# Driver program
41+
if __name__=='__main__':
42+
arr1 = ['P', 'T', 'T', 'P', 'T']
43+
k = 2
44+
n = len(arr1)
45+
print(("Maximum thieves caught: {}".
46+
format(policeThief(arr1, n, k))))
47+
48+
arr2 = ['T', 'T', 'P', 'P', 'T', 'P']
49+
k = 2
50+
n = len(arr2)
51+
print(("Maximum thieves caught: {}".
52+
format(policeThief(arr2, n, k))))
53+
54+
arr3 = ['P', 'T', 'P', 'T', 'T', 'P']
55+
k = 3
56+
n = len(arr3)
57+
print(("Maximum thieves caught: {}".
58+
format(policeThief(arr3, n, k))))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /