Solution O(n)

  • 0

    According to this website, in the end, only the numbers that are perfect squares remain On, since other divisors come in pairs, so if one turns a light on, the other will turn it off.

    class Solution(object):
        def bulbSwitch(self, n):
            :type n: int
            :rtype: int
            for i in xrange(1, n+1):
                if i ** 2 <= n:
                    res +=1 
            return res

Log in to reply

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