why is this a Greedy algorithm problem? it’s more of a brain teaser.
the fact that i am switching on the first off bulb is greedy nature? Is there a non-greedy way of solving it?
My sol:
class Solution:
# @param A : list of integers
# @return an integer
def bulbs(self, A):
count = 0
flag = True
# if any bulb switched twice, it will come back to original state
for bulb in A:
if bulb == 0 and flag: #this bulb in original state
count += 1
flag = False
elif bulb ==1 and not flag: #this bulb not in orig state, i.e now in off mode
count += 1
flag = True
return count