My 0ms java solution with explanation


  • 1
    public class Solution {
    public int bulbSwitch(int n) {
        int i=0;
        for(i=1;i*i<=n;i++){
        }
            return i-1;
    }
    

    }

    The basic idea is to count how many numbers in the range of [1, n] have odd factors. For in the i round, the buld on the position k, which i is a factor of k, will be switched.

    If the buld in the position k is on finally, k has odd factors. Only the numbers in this situation: { i | i*i=n, i is an integer} has odd factors.

    bulbSwitch(N)=n , n*n=<N<(n+1)(n+1)
    =square(N)


  • 0
    H

    Great idea, but you may need to be more clear as for description: the idea is to count how many numbers in the range [1, n] that have odd NUMBER of factors. Otherwise it can be a little confusing, at least for me.


Log in to reply
 

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