Sunday, December 20, 2015

Bulb Switcher [Leetcode]

Problem Description
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.
Example:
Given n = 3. 

At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off]. 

So you should return 1, because there is only one bulb is on.
Solution

It is interesting that LeetCode just tried to introduce brainteaser problems.
For these types of problems, we just need to keep thinking simple, then we can solve the problem easily. In stead of imagining about all the n bulbs, we can consider 1 bulb only. Is the ith bulb on or off after n rounds? We see that the ith bulb is switched (on to off, or off to on) in the kth round if and only if i % k == 0. Initially the ith is on, so if i's number of divisors is odd, it will be on after n rounds, otherwise it is off.
Therefore, the importance question is: How many integer from 1 to n (inclusively) having odd numbers of divisors? 
We observe that if k | i then (i/k) | i . Hence, if k == i/k, or i is a square, then i has a odd number of divisors. And the number of squares less than or equal to n is floor ( sqrt (n)).
So we have the following simple code, which is accepted:
public class Solution {
    public int bulbSwitch(int n) {
        return (int) Math.sqrt(n);
    }
}
Is that short & good enough? Maybe. And we can move on to other problems in Leetcode or other sites. However, what I want to emphasize here is that, if we don't feel satisfied with the solution we have, we will eventually acquired more knowledge. ^_^. For example, in this problem, if we try to find another way to compute the result (actually it is Integer Square Root of n ), we will find many methods. One of them is below credited to this stackoverflow question.
public class Solution {
    public int bulbSwitch(int n) {
        return isqrt(n);
    }
    public int isqrt(int n){
        int op  = n;
        int res = 0;
        int one = 1 << 30; // The second-to-top bit is set: use 1u << 14 for uint16_t type; use 1uL<<30 for uint32_t type
    
    
        // "one" starts at the highest power of four <= than the argument.
        while (one > op)
        {
            one >>= 2;
        }
    
        while (one != 0)
        {
            if (op >= res + one)
            {
                op = op - (res + one);
                res = res +  2 * one;
            }
            res >>= 1;
            one >>= 2;
        }
        return res;
    }
}
REFERENCES
Efficient Integer Square Root for large number.
Square Root.
Comparison between different square root methods.

No comments:

Post a Comment