44ms Python using perfect squares

  • -1

    The only bulbs that will be 'on' after all passes are completed will be the perfect squares. So, we just find however many perfect squares there are that are <= n:

    class Solution(object):
        def bulbSwitch(self, n):
            :type n: int
            :rtype: int
            i = 1
            while i ** 2 <= n:
                i += 1
            return i - 1

Log in to reply

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