-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from sahil2128/master
New Problem Statement
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |