My fastest "not Math" java solution n=10,000,000 t=1071ms


  • 0
    S

    My fastest "not Math" solution n=10,000,000 t=1071ms

    public int bulbSwitch(int n) {
        if(n<2){
            return n;
        }
        int result = 0;
    
        boolean[] bulbs = new boolean[n+1];
        bulbs[1]=true;
        for(int swch = 2; swch<=n/2; swch++){
            for(int bulb = 2*swch; bulb<n+1; bulb+=swch){
                bulbs[bulb] = !bulbs[bulb];
            }
        }
    
        for(int i = 1; i<=n; i++){
            if(bulbs[i]){
                result++;
            }
        }
        return result;
    }

Log in to reply
 

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