Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

672. Bulb Switcher II - Leetcode Solution

Code Implementation

class Solution:
    def flipLights(self, n: int, presses: int) -> int:
        if presses == 0:
            return 1
        if n == 1:
            return 2
        if n == 2:
            return 3 if presses == 1 else 4
        if presses == 1:
            return 4
        if presses == 2:
            return 7
        return 8
      
class Solution {
public:
    int flipLights(int n, int presses) {
        if (presses == 0) return 1;
        if (n == 1) return 2;
        if (n == 2) return presses == 1 ? 3 : 4;
        if (presses == 1) return 4;
        if (presses == 2) return 7;
        return 8;
    }
};
      
class Solution {
    public int flipLights(int n, int presses) {
        if (presses == 0) return 1;
        if (n == 1) return 2;
        if (n == 2) return presses == 1 ? 3 : 4;
        if (presses == 1) return 4;
        if (presses == 2) return 7;
        return 8;
    }
}
      
var flipLights = function(n, presses) {
    if (presses === 0) return 1;
    if (n === 1) return 2;
    if (n === 2) return presses === 1 ? 3 : 4;
    if (presses === 1) return 4;
    if (presses === 2) return 7;
    return 8;
};
      

Problem Description

You are given n bulbs, all initially turned on. There are four different switches that can flip the bulbs in the following ways:

  • Flip all the bulbs.
  • Flip bulbs with even numbers.
  • Flip bulbs with odd numbers.
  • Flip bulbs with positions of the form 3k + 1 (i.e., bulb 1, 4, 7, ...).

Each switch can be pressed any number of times, but you can press a total of at most presses times in any order (even pressing the same switch more than once).

Your task is to return the number of different possible statuses (on/off configurations) of the bulbs after performing at most presses operations. Each bulb is either on or off.

Constraints:

  • 1 ≤ n ≤ 1000
  • 0 ≤ presses ≤ 1000

Thought Process

At first glance, it might seem that there are a huge number of possible states, especially with large n and presses. Brute-forcing all possible press sequences is impractical. However, by analyzing the nature of the switches and how they interact, we can see that:

  • Each switch toggles certain bulbs in a pattern.
  • Pressing the same switch twice is equivalent to not pressing it at all (since toggling twice returns to the original state).
  • The order of presses does not matter, only which switches are pressed and how many times.
  • Because of the symmetry and periodicity of the bulb toggling patterns, only the first few bulbs matter for determining all unique states.

This leads us to consider combinations of switch presses rather than sequences, and also to realize that for n > 6, the number of unique states does not increase.

Solution Approach

  • Reduction to Small n: Because the toggling patterns repeat every 6 bulbs, we only need to consider the first 6 bulbs, regardless of how large n is.
  • Switch Press Combinations: Each switch can be pressed 0 or 1 time (since pressing twice cancels out). Thus, there are at most 24 = 16 possible combinations of switch presses.
  • Press Limit: Only consider combinations with k presses where kpresses and k has the same parity as presses (since extra presses can be wasted by pressing the same switch twice).
  • Enumerate States: For each valid combination, compute the resulting bulb state and collect unique states in a set.
  • Special Cases: For small n and presses, the number of possible states is less than 8. Hardcode these for efficiency.
  • Return Result: The answer is the number of unique states.

In practice, the number of unique states is:

  • 1 if presses == 0
  • 2 if n == 1 and presses > 0
  • 3 or 4 if n == 2
  • 4, 7, or 8 for n >= 3 depending on presses

Example Walkthrough

Let's take n = 3 and presses = 1 as an example:

  • Starting state: [on, on, on]
  • Four possible single-press actions:
    • Flip all: [off, off, off]
    • Flip even: [on, off, on]
    • Flip odd: [off, on, off]
    • Flip 3k+1: [off, on, on]
  • These are 4 distinct states, so the answer is 4.

For n = 2 and presses = 1:

  • Possible states: [off, off], [on, off], [off, on]
  • There are 3 distinct states.

Time and Space Complexity

  • Brute-force: Would be O(4^presses * n), since each press has 4 choices and each state is n bulbs. This is infeasible for large presses.
  • Optimized: There are at most 16 possible switch combinations (since each can be pressed at most once), and for each, we compute the state for up to 6 bulbs. So the time and space complexity is O(1) — constant time.

The solution is extremely efficient due to the combinatorial reduction and symmetry in the problem.

Summary

The key insight is that the number of unique bulb configurations is limited by the patterns of the switches, not by the size of n or presses. By analyzing the toggling effects and using combinatorics, we reduce the problem to a handful of cases. This makes the solution both elegant and highly efficient, suitable for even the largest constraints.