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 ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.
Return the number of bulbs that are on after n rounds.
Solution
Each position is only switched on its factor rounds, and factors appear in pairs except when squared, where the factor is the same. Therefore, the problem is equivalent to finding the number of times a perfect square appears.
classSolution { public: intbulbSwitch(int n){ // bulbs will switch in factor round // since factor round will appear with pair // we just need to count square round
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
sum -= max; max = max - sum; if(sum <= 0 || max <= 0) returnfalse; intn=1; while(max > sum){ max -= n * sum; if(max <= 0){ max += n * sum; break; } n*=2; } sum += max; pq.offer(max); } } }