Created
July 20, 2018 08:09
-
-
Save vedant1811/9f08cffbf96726df0f12ad0c121e0656 to your computer and use it in GitHub Desktop.
Better solution to https://www.geeksforgeeks.org/policemen-catch-thieves/
This file contains hidden or 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
def catch(arr, k) | |
ps = 0 | |
ts = 0 | |
count = 0 | |
arr.each do |e| | |
if e == 'T' | |
if ps > 0 | |
ps -= 1 | |
count += 1 | |
else | |
ts += 1 unless ts >= k | |
end | |
else | |
if ts > 0 | |
ts -= 1 | |
count += 1 | |
else | |
ps += 1 unless ps >= k | |
end | |
end | |
end | |
count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment