672. Bulb Switcher II LeetCode Solution
In this guide, you will get 672. Bulb Switcher II LeetCode Solution with the best time and space complexity. The solution to Bulb Switcher II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Bulb Switcher II solution in C++
- Bulb Switcher II solution in Java
- Bulb Switcher II solution in Python
- Additional Resources
Problem Statement of Bulb Switcher II
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
Constraints:
1 <= n <= 1000
0 <= presses <= 1000
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
672. Bulb Switcher II LeetCode Solution in C++
class Solution {
public:
int flipLights(int n, int m) {
n = min(n, 3);
if (m == 0)
return 1;
if (m == 1)
return vector{2, 3, 4}[n - 1];
if (m == 2)
return vector{2, 4, 7}[n - 1];
return pow(2, n);
}
};
/* code provided by PROGIEZ */
672. Bulb Switcher II LeetCode Solution in Java
class Solution {
public int flipLights(int n, int m) {
n = Math.min(n, 3);
if (m == 0)
return 1;
if (m == 1)
return new int[] {2, 3, 4}[n - 1];
if (m == 2)
return new int[] {2, 4, 7}[n - 1];
return (int) Math.pow(2, n);
}
}
// code provided by PROGIEZ
672. Bulb Switcher II LeetCode Solution in Python
class Solution:
def flipLights(self, n: int, m: int) -> int:
n = min(n, 3)
if m == 0:
return 1
if m == 1:
return [2, 3, 4][n - 1]
if m == 2:
return [2, 4, 7][n - 1]
return [2, 4, 8][n - 1]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.