LeetCode 319 Bulb Switcher (Bulb switching) (algorithm found from law ......)
Translation
N bulbs are initialized here. First, you light all the bulbs. Then you turn off the second bulb. In the third round, you switch each third light bulb (enable it if it is disabled, and disable it if it is enabled ). For the I-th back, you switch each I-th bulb. For the nth return, you only need to switch the last bulb. Find out how many bulbs are on after n rounds.
Original
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. 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.
Analysis
I am in the first stage of this question
I cannot even understand the question. What is every second bulb? There is not a two-dimensional array (matrix). If we understand every second bulb as second bulb, isn't the first lamp always on?
So try writing a function at will:
class Solution {public: int bulbSwitch(int n) { if(n == 0) return 0; return 1; }};
So LeetCode tells me that if the input is 4, it should return 2 ...... Continue:
class Solution {public: int bulbSwitch(int n) { if(n == 0) return 0; if(n == 4) return 2; return 1; }};
It also tells me whether to return 2 If the input is 5.
So I figured it out on the paper. The conclusion is that every two of them means the second, the fourth, and the sixth ......
I am in the second stage of this question
Based on the above ideas, I wrote the following algorithms, which are implemented with poor experience. LeetCode also tells me that if it is 999999 or something, it will time out ......
int bulbSwitch(int n) { bool* bulb = new bool[n]; for (int i = 1; i <= n; ++i) { if (i == 1) { for (int j = 0; j < n; ++j) bulb[j] = true; } if (i > 1 && i < n) { for (int j = i; j < n; ) { bulb[j - 1] = bulb[j - 1] == true ? false : true; j += i; } } if (i == n) { bulb[n - 1] = bulb[n - 1] == true ? false : true; } } int result = 0; for (int i = 0; i < n; ++i) { if (bulb[i] == true) result += 1; } return result;}
I am in the third stage of this question
So I tried to use bitwise operations to replace an array of the bool type with a binary number. The values 0 and 1 in the binary represent the bool array, so that the storage space is small, high efficiency. However, I have not finished ...... It's really complicated. You can try again later.
I am in the fourth stage of this question
When bit operations fail, you want to take shortcuts. In order to clearly see the changes in the process, all of them are printed:
7. What else can't be seen ......
20, suddenly found the bottom, ah, a little interesting ......
I continued to try for 30, so I firmly believed this was the case.
1 + 2 + 1 + 4 + 1 + 6 + 1 + 8 + 1 + 10 + 1
I refer to this as a carry-on car, where "2, 4, 6, 8 ......" It is called a wheel.
int wheel = 2; int result = 0; int trackvehicle = 0; bool isNotWheel = true;
The first wheel starts from 2, and the result is the final result. trackvehicle is the length of the whole crawler, isNotWheel is not the first wheel (the second and fourth are the wheels ).
OK, the logic is also very simple, and the code is directly ......
Code
class Solution {public:int bulbSwitch(int n) { int wheel = 2; int result = 0; int trackvehicle = 0; bool isNotWheel = true; while ( trackvehicle < n ) { if (isNotWheel) { trackvehicle += 1; result += 1; isNotWheel = !isNotWheel; } else { trackvehicle += wheel; wheel += 2; isNotWheel = !isNotWheel; } } return result;}};