# Solving Leetcode Interviews in Seconds with AI: Bulb Switcher II


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "672" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# 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    Constraints:  1 <= n <= 1000 0 <= presses <= 1000  

	# Explanation
	Here's the breakdown of the approach, complexities, and the Python code:

*   **High-Level Approach:**
    *   **State Reduction:** The key is to realize that the effect of the buttons repeats after a certain number of presses and the buttons are not entirely independent. We can reduce the number of presses by taking `presses % 2` if `presses > 2` and simplify the problem by considering only the first few bulbs since the pattern extends.
    *   **Enumerate Combinations:** Generate all possible combinations of button presses using bit manipulation. Iterate through all 2<sup>4</sup> possible combinations and apply the corresponding button presses.
    *   **Store and Count Distinct States:**  Store the resulting bulb configurations as tuples (immutable) in a `set` to automatically eliminate duplicates and count the unique states.

*   **Complexity:**
    *   Runtime Complexity: O(2<sup>4</sup>) = O(1) as the number of operations is bounded by the button combinations.
    *   Storage Complexity: O(2<sup>min(n, 6)</sup>) as the number of distinct bulb states.

	
	# Code
	```python
	def flipLights(n: int, presses: int) -> int:
    """
    Calculates the number of distinct bulb configurations after a given number of button presses.

    Args:
        n: The number of bulbs.
        presses: The number of button presses.

    Returns:
        The number of distinct bulb configurations.
    """

    seen = set()

    for i in range(min(presses + 1, 4)):  # Iterate through relevant press counts
        for mask in range(1 << 4):  # Iterate through all combinations of button presses
            count = bin(mask).count('1')
            if count % 2 == i % 2 and count <= i:
                lights = [1] * min(n, 6) # Limit bulbs to 6, after that pattern is fixed.

                if mask & 1:  # Button 1
                    for j in range(len(lights)):
                        lights[j] ^= 1
                if mask & 2:  # Button 2
                    for j in range(1, len(lights), 2):
                        lights[j] ^= 1
                if mask & 4:  # Button 3
                    for j in range(0, len(lights), 2):
                        lights[j] ^= 1
                if mask & 8:  # Button 4
                    for j in range(0, len(lights)):
                        if (j + 1) % 3 == 1:
                            lights[j] ^= 1
                seen.add(tuple(lights))

    return len(seen)
	```
			
