Simple Java solution square root

  • 3

    Only bulbs with index equaling a perfect square number will be on in the end. (because it has an odd number of divisors). So a simple solution follows:

    public static int bulbSwitch(int n) {
            return (int)Math.sqrt((double)n);

Log in to reply

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