Simulation approach, want to share, correct but not accepted(time exceeded)


  • 0
    K

    Yes, I know the proper math solution, but this simulates the situation and you can print out the array of light bulbs to pick up the pattern initially.

    class Solution(object):
        def bulbSwitch(self, n):
            """
            :type n: int
            :rtype: int
            """
            bulbs= [0]*n #0 for off state
            for step in range(1, n+1):
                for s in range(step-1,  n, step):
                    bulbs[s]=0 if bulbs[s]==1 else 1
            count=0
    
            for b in bulbs: # count the number of on light bulbs (1's) in array
                if b == 1:
                    count+=1
            return count
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.