Last updated on February 28th, 2025 at 01:22 pm
Here, we see a Bulb Switcher II LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Math
Companies
Microsoft
Level of Question
Medium

Bulb Switcher II LeetCode Solution
Table of Contents
1. Problem Statement
There is a room with n bulbs labeled from 1 to n that all are turned on initially, and four buttons on the wall. Each of the four buttons has a different functionality where:
- Button 1: Flips the status of all the bulbs.
- Button 2: Flips the status of all the bulbs with even labels (i.e., 2, 4, …).
- Button 3: Flips the status of all the bulbs with odd labels (i.e., 1, 3, …).
- Button 4: Flips the status of all the bulbs with a label j = 3k + 1 where k = 0, 1, 2, … (i.e., 1, 4, 7, 10, …).
You must make exactly presses button presses in total. For each press, you may pick any of the four buttons to press.
Given the two integers n and presses, return the number of different possible statuses after performing all presses button presses.
Example 1:
Input: n = 1, presses = 1
Output: 2
Explanation: Status can be:
– [off] by pressing button 1
– [on] by pressing button 2
Example 2:
Input: n = 2, presses = 1
Output: 3
Explanation: Status can be:
– [off, off] by pressing button 1
– [on, off] by pressing button 2
– [off, on] by pressing button 3
Example 3:
Input: n = 3, presses = 1
Output: 4
Explanation: Status can be:
– [off, off, off] by pressing button 1
– [off, on, off] by pressing button 2
– [on, off, on] by pressing button 3
– [off, on, on] by pressing button 4
2. Coding Pattern Used in Solution
The provided coding pattern is a “Decision Tree Reduction” pattern. This pattern involves reducing a problem into a small set of possible outcomes based on the constraints and inputs. The code uses a series of conditional checks (if
statements) to determine the number of unique light configurations based on the number of bulbs (n
) and the number of presses (presses
).
3. Code Implementation in Different Languages
3.1 Bulb Switcher II C++
class Solution { public: int flipLights(int n, int presses) { if(presses == 0) return 1; if(n == 1) return 2; if(n == 2 && presses == 1) return 3; if(n == 2 || presses == 1) return 4; if(presses == 2) return 7; return 8; } };
3.2 Bulb Switcher II Java
class Solution { public int flipLights(int n, int presses) { if(presses==0) return 1; if(n==1) return 2; if(n==2 && presses==1) return 3; if(n==2) return 4; if(presses==1) return 4; if(presses==2) return 7; if(presses>=3) return 8; return 8; } }
3.3 Bulb Switcher II JavaScript
var flipLights = function(n, presses) { if (presses === 0) { return 1; } else if (n === 1) { return 2; } else if (n === 2) { return presses === 1 ? 3 : 4; } else { return presses === 1 ? 4 : presses === 2 ? 7 : 8; } };
3.4 Bulb Switcher II Python
class Solution(object): def flipLights(self, n, presses): if(presses==0): return 1; if(n==1): return 2; if(n==2 and presses==1): return 3; if(n==2): return 4; if(presses==1): return 4; if(presses==2): return 7; if(presses>=3): return 8; return 8;
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(1) | O(1) |
Java | O(1) | O(1) |
JavaScript | O(1) | O(1) |
Python | O(1) | O(1) |
- The code is highly optimized for this specific problem and does not involve loops or recursion.
- The number of bulbs (
n
) and the number of presses (presses
) are reduced to a small set of cases, making the solution efficient. - The logic is based on mathematical reasoning about the symmetry and periodicity of the bulb states.