Related:
LeetCode 319 - Bulb Switcher
LeetCode 672 - Bulb Switcher II
[LeetCode]Bulb Switcher | 书影博客
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:
https://leetcode.com/problems/bulb-switcher/discuss/77104/Math-solution..
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 theith 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 aftern 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 ihas a odd number of divisors. And the number of squares less than or equal ton is floor ( sqrt (n)).
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.
Square Root.
Comparison between different square root methods.
Read full article from [LeetCode]Bulb Switcher | 书影博客
LeetCode 319 - Bulb Switcher
LeetCode 672 - Bulb Switcher II
[LeetCode]Bulb Switcher | 书影博客
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:
https://leetcode.com/problems/bulb-switcher/discuss/77104/Math-solution..
A bulb ends up on iff it is switched an odd number of times.
Call them bulb 1 to bulb n. Bulb i is switched in round d if and only if d divides i. So bulb i ends up on if and only if it has an odd number of divisors.
Divisors come in pairs, like i=12 has divisors 1 and 12, 2 and 6, and 3 and 4. Except when i is a square, like 36 has divisors 1 and 36, 2 and 18, 3 and 12, 4 and 9, and double divisor 6. So bulb i ends up on if and only if i is a square.
So just count the square numbers.
Let R = int(sqrt(n)). That's the root of the largest square in the range [1,n]. And 1 is the smallest root. So you have the roots from 1 to R, that's R roots. Which correspond to the R squares. So int(sqrt(n)) is the answer. (C++ does the conversion to int automatically, because of the specified return type)
int bulbSwitch(int n) {
return sqrt(n);
}
https://leetcode.com/problems/bulb-switcher/discuss/77112/Share-my-o(1)-solution-with-explanation
数学题,答案等于
int(math.sqrt(n))
为什么只有完全平方数的因子个数为奇数呢?
因为除了完全平方数,其余数字的因子都是成对出现的,而完全平方数的平方根只会统计一次。
前10盏灯泡的开闭过程如下所示:
Python代码:
http://traceformula.blogspot.com/2015/12/bulb-switcher-leetcode.htmlFor 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 theith 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 aftern 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 ihas a odd number of divisors. And the number of squares less than or equal ton is floor ( sqrt (n)).
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 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;
- }
Square Root.
Comparison between different square root methods.
Read full article from [LeetCode]Bulb Switcher | 书影博客