Skip to content

Commit

Permalink
Merge pull request #75 from sahil2128/master
Browse files Browse the repository at this point in the history
New Problem Statement
  • Loading branch information
manishbisht authored Jul 23, 2021
2 parents 214077d + 80c5fcc commit 039ad55
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Daily Coding Problem/00526/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Policemen catch thieves

Given an array of size n that has the following specifications:

Each element in the array contains either a policeman or a thief.
Each policeman can catch only one thief.
A policeman cannot catch a thief who is more than K units away from the policeman.
We need to find the maximum number of thieves that can be caught.
Examples:

```bash
Input : arr[] = {'P', 'T', 'T', 'P', 'T'},
k = 1.
Output : 2.
```
Here maximum 2 thieves can be caught, first
policeman catches first thief and second police-
man can catch either second or third thief.

```
Input : arr[] = {'T', 'T', 'P', 'P', 'T', 'P'},
k = 2.
Output : 3.
```
```
Input : arr[] = {'P', 'T', 'P', 'T', 'T', 'P'},
k = 3.
Output : 3.
```
58 changes: 58 additions & 0 deletions Daily Coding Problem/00526/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Python3 program to find maximum
# number of thieves caught

# Returns maximum number of thieves
# that can be caught.
def policeThief(arr, n, k):
i = 0
l = 0
r = 0
res = 0
thi = []
pol = []

# store indices in list
while i < n:
if arr[i] == 'P':
pol.append(i)
elif arr[i] == 'T':
thi.append(i)
i += 1

# track lowest current indices of
# thief: thi[l], police: pol[r]
while l < len(thi) and r < len(pol):

# can be caught
if (abs( thi[l] - pol[r] ) <= k):
res += 1
l += 1
r += 1

# increment the minimum index
elif thi[l] < pol[r]:
l += 1
else:
r += 1

return res

# Driver program
if __name__=='__main__':
arr1 = ['P', 'T', 'T', 'P', 'T']
k = 2
n = len(arr1)
print(("Maximum thieves caught: {}".
format(policeThief(arr1, n, k))))

arr2 = ['T', 'T', 'P', 'P', 'T', 'P']
k = 2
n = len(arr2)
print(("Maximum thieves caught: {}".
format(policeThief(arr2, n, k))))

arr3 = ['P', 'T', 'P', 'T', 'T', 'P']
k = 3
n = len(arr3)
print(("Maximum thieves caught: {}".
format(policeThief(arr3, n, k))))

0 comments on commit 039ad55

Please sign in to comment.