|
| 1 | +id: bulb-switcher |
| 2 | +title: Bulb Switcher (LeetCode) |
| 3 | +sidebar_label: 319-Bulb-Switcher |
| 4 | +tags: |
| 5 | + - Math |
| 6 | + - Brain Teaser |
| 7 | +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 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. |
| 8 | + |
| 9 | +sidebar_position: 0319 |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +Return the number of bulbs that are on after n rounds. |
| 19 | + |
| 20 | +### Example 1 |
| 21 | + |
| 22 | +- **Input:** `3` |
| 23 | +- **Output:** `1` |
| 24 | +- **Explanation:** |
| 25 | + - At first, the three bulbs are [off, off, off]. |
| 26 | + - After the first round, the three bulbs are [on, on, on]. |
| 27 | + - After the second round, the three bulbs are [on, off, on]. |
| 28 | + - After the third round, the three bulbs are [on, off, off]. |
| 29 | +So you should return 1 because there is only one bulb is on. |
| 30 | + |
| 31 | +### Example 2 |
| 32 | + |
| 33 | +- **Input:** `0` |
| 34 | +- **Output:** `0` |
| 35 | + |
| 36 | +### Example 2 |
| 37 | + |
| 38 | +- **Input:** `1` |
| 39 | +- **Output:** `1` |
| 40 | + |
| 41 | +### Constraints |
| 42 | + |
| 43 | +- `0 <= n <= 10^9` |
| 44 | + |
| 45 | +## Approach |
| 46 | +- We can notice that a bulb's final state (on or off) depends on how many times it is toggled. Bulbs are toggled only when their positions are factors of the round number. So, the crucial insight is that a bulb ends up being on if and only if it has an odd number of factors. Mathematically, the only numbers that have an odd number of factors are perfect squares because factors usually come in pairs, and a square has a middle factor that is counted only once. For example, 9 is toggled on rounds 1, 3, and 9. |
| 47 | + |
| 48 | +- Given this, the problem simplifies to finding out how many perfect squares are there up to n, because these will be the bulbs that remain on. The number of perfect squares up to n is simply the largest integer square root of n, because for each number x such that `x^2 <= n`, there is a corresponding perfect square. |
| 49 | + |
| 50 | +### Solution Code |
| 51 | + |
| 52 | +#### C++ |
| 53 | + |
| 54 | +```c++ |
| 55 | +class Solution { |
| 56 | +public: |
| 57 | + int bulbSwitch(int n) { |
| 58 | + return sqrt(n); |
| 59 | + } |
| 60 | +}; |
| 61 | +``` |
| 62 | +#### Java |
| 63 | +```java |
| 64 | +class Solution { |
| 65 | + public int bulbSwitch(int n) { |
| 66 | + return (int) Math.sqrt(n); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +#### Python |
| 72 | +```python |
| 73 | +class Solution: |
| 74 | + def bulbSwitch(self, n: int) -> int: |
| 75 | + return int(sqrt(n)) |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | +#### Conclusion |
| 80 | +- Time Complexity |
| 81 | +The total time complexity as O(1). |
| 82 | + |
| 83 | +- Space Complexity |
| 84 | +The space complexity is O(1). |
0 commit comments