You are given an array bulbs of integers between 1 and 100.
There are 100 light bulbs numbered from 1 to 100. All of them are switched off initially.
For each element bulbs[i] in the array bulbs:
bulbs[i]th light bulb is currently off, switch it on.Return the list of integers denoting the light bulbs that are on in the end, sorted in ascending order. If no bulb is on, return an empty list.
Example 1:
Input: bulbs = [10,30,20,10]
Output: [20,30]
Explanation:
bulbs[0] = 10th light bulb is currently off. We switch it on.bulbs[1] = 30th light bulb is currently off. We switch it on.bulbs[2] = 20th light bulb is currently off. We switch it on.bulbs[3] = 10th light bulb is currently on. We switch it off.Example 2:
Input: bulbs = [100,100]
Output: []
Explanation:
bulbs[0] = 100th light bulb is currently off. We switch it on.bulbs[1] = 100th light bulb is currently on. We switch it off.Constraints:
1 <= bulbs.length <= 1001 <= bulbs[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to solving the toggle light bulbs problem involves physically following the exact rules of the puzzle step-by-step for every single bulb. We literally walk down the line of bulbs over and over, flipping the correct ones just like the instructions tell us to, until we have gone through every round of changes.
Here's how the algorithm would work step-by-step:
def toggle_light_bulbs_brute_force(total_bulbs):
light_bulb_states = [False] * total_bulbs
# Iterate through each round up to the total number of bulbs to simulate the exact walking steps
for current_round in range(1, total_bulbs + 1):
for bulb_index in range(current_round - 1, total_bulbs, current_round):
light_bulb_states[bulb_index] = not light_bulb_states[bulb_index]
glowing_bulb_count = 0
# Count the final glowing bulbs to determine the total outcome of our simulation rounds
for bulb_state in light_bulb_states:
if bulb_state:
glowing_bulb_count += 1
return glowing_bulb_countInstead of physically flipping the light bulbs one by one, the optimal approach relies on a clever mathematical pattern to figure out which ones remain on. By thinking about how many times each bulb gets touched, you can instantly know its final state without doing any heavy lifting.
Here's how the algorithm would work step-by-step:
def toggle_light_bulbs(total_bulb_count):
# Perfect squares have an odd number of divisors, leaving them turned on.
glowing_bulbs_list = []
current_integer = 1
# Only check numbers whose squares do not exceed the total bulb count.
while (current_integer * current_integer) <= total_bulb_count:
glowing_bulbs_list.append(current_integer * current_integer)
current_integer += 1
# Return the exact positions of all bulbs that remain glowing.
return glowing_bulbs_list| Case | How to Handle |
|---|---|
| n is equal to 0 or negative | Return 0 immediately because there are no light bulbs to operate on. |
| n is equal to 1 | Return 1 since the single bulb is toggled once in the first round and remains on. |
| n is a very large integer up to the 32-bit signed integer limit | Use a mathematical square root approach to calculate the result in O(1) time and avoid Time Limit Exceeded errors. |
| n is a perfect square | The integer square root will be exact, and the bulb will remain on because it gets toggled an odd number of times. |
| n is not a perfect square | Taking the integer square root via floor operation accurately yields the exact count of bulbs left on. |
| n is extremely large where naive simulation would cause memory or time limits | The O(1) math solution bypasses any memory allocation and prevents simulation bottlenecks entirely. |
| Language-specific integer overflow during intermediate calculations | Using standard integer types is sufficient since operations involve simple square roots well within standard limits. |