It's equivalent to count the square numbers no larger than n


  • 4
    F

    /* For individual bulb, its final state depends on how many times it got toggled, and it equals the number of its distinct factors. If the number is even, the bulb is off, otherwise it's on. We know all numbers factors are even number unless it's a square number. So the problem is equivalent to count the square numbers no larger than n
    */

    public class Solution {
        public int BulbSwitch(int n) {
            return (int)Math.Sqrt(n);
        }
    }

Log in to reply
 

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