class solution: def solve(self, A, B): i, n, bulb = 0, len(A), 0 while i < n: flag = False j = min(i+B-1, n-1) while j>=i-B+1 and j<n and j>0: flag = True bulb += 1 i = j + B break j -= 1 if not flag: return -1 return bulb
j = min(i+B-1, n-1) what this line mean , please explain
it means, it will keep in boundation of length of an array. (i+B-1) is the maximum distance of glow light, if sometimes i+B-1 gives greater value than length of array then it will min() function takes n-1 value instead of i+B-1.
suppose for an example, n=8, B=3 and at iteration value i=4
value of i+B-1 = 4+3-1 = 6 here 6<(n-1) so j will take 6. but when iteration value iterate at i=6 then,
i+B-1 = 6+3-1 = 8 where 8>(n-1) this is out of size of an array then j will takes 7 instead of 8 because it means light glow distance is at last index of array.
j = min(i+B-1, n-1)
this function is use for maintaining the length boundation of an array.
def solve(self, A, B): i, n, bulb = 0, len(A), 0 while i < n: flag = False j = min(i+B-1, n-1) while j>=i-B+1 and j<n and j>0: if A[j] ==1: flag = True bulb += 1 i = j + B break j -= 1 if not flag: return -1 return bulb
Your code does not checks the condition of whether the bulb is working(i.e 1 ) or not. and also decrementing the j should be inside the while loop
please recheck once again the code. it accepting all the conditions which fulfil the question.
j is already inside the while loop. or if you have any strong TC then please share that one which fail the case of this code.
This is the code that I modified and shared I am talking about your code, where if A[j] == 1: the condition is missing