Number of Water Tanks Needed | Interview

Stuck on this question I got during an interview.
Suppose you have an array of houses "-H-H-H", each house needs to be close to a water tank on either its left or right side. How many water tanks do you need? Water Tanks can only go where there is a dash.

Ex. "-H-H-H" -> solution: 2 tanks needed, one option is '-HTHTH', or 'TH-HTH'

The solution I used is to place a water tank in between each pair of unserved houses that has exactly 1 dash between them, then after that, just assigned a water tank to the left or right of all the remaining unserved houses.

s="-H-H-H"
L=list(s)
N=len(L)
House_count=L.count('H')
for i in range(1,N-1):
if(L[i-1]=='H' and L[i-1]!='F' and L[i]=='-' and L[i+1]=='H' and L[i+1]!='F'):
L[i-1],L[i+1]='F','F'
L[i]='T'
print(L)
for i in range(1,N-1):
if(L[i-1]=='H' and L[i-1]!='F' and L[i]=='-'):
L[i-1]='F'
L[i]='T'
elif(L[i+1]=='H' and L[i+1]!='F' and L[i]=='-'):
L[i+1]='F'
L[i]='T'
Found_count=L.count('F')
Tank_count=L.count('T')
print(L)
if(Tank_count==0 or Found_count!=House_count):
print(-1)
else:
print(Tank_count)

The code passed only 5/8 test cases I tried all the combinations from my side but still can't figure out what I'm doing wrong. Any help would be appricieated.

Comments (1)