KatInterview

672. Bulb Switcher II

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the number of unique final states of n light bulbs after exactly presses button presses. The key observations are:

Solution

Bulb Switcher II in Python

class Solution:
    def flipLights(self, n: int, presses: int) -> int:
        # If no presses are made, all bulbs remain on. Only 1 state.
        if presses == 0:
            return 1

        # For n=1, there are only two states: ON or OFF.
        # With 1 press, we can turn it OFF (b1, b3, b4) or leave it ON (b2).
        # With more presses, both ON and OFF states are achievable.
        if n == 1:
            return 2

        # For n=2, there are 4 possible states: (ON,ON), (ON,OFF), (OFF,ON), (OFF,OFF).
        # With 1 press: we can get (OFF,OFF) by b1, (ON,OFF) by b2, (OFF,ON) by b3 or b4. Total 3 states.
        # With 2 or more presses, all 4 states are achievable.
        if n == 2:
            if presses == 1:
                return 3
            else:
                return 4

        # For n>=3, the number of states saturates.
        # With 1 press: 4 distinct operations (b1, b2, b3, b4) lead to 4 states.
        # With 2 presses: 7 distinct operations are possible, leading to 7 states.
        # With 3 or more presses: all 8 distinct operations are possible, leading to 8 states.
        if n >= 3:
            if presses == 1:
                return 4
            elif presses == 2:
                return 7
            else:
                return 8
        
        # This part should not be reached due to constraints n>=1
        return 0

Bulb Switcher II in Java

class Solution {
    public int flipLights(int n, int presses) {
        // If no presses are made, all bulbs remain on. Only 1 state.
        if (presses == 0) {
            return 1;
        }

        // For n=1, there are only two states: ON or OFF.
        // With 1 press, we can turn it OFF (b1, b3, b4) or leave it ON (b2).
        // With more presses, both ON and OFF states are achievable.
        if (n == 1) {
            return 2;
        }

        // For n=2, there are 4 possible states: (ON,ON), (ON,OFF), (OFF,ON), (OFF,OFF).
        // With 1 press: we can get (OFF,OFF) by b1, (ON,OFF) by b2, (OFF,ON) by b3 or b4. Total 3 states.
        // With 2 or more presses, all 4 states are achievable.
        if (n == 2) {
            if (presses == 1) {
                return 3;
            }
            return 4;
        }

        // For n>=3, the number of states saturates.
        // With 1 press: 4 distinct operations (b1, b2, b3, b4) lead to 4 states.
        // With 2 presses: 7 distinct operations are possible, leading to 7 states.
        // With 3 or more presses: all 8 distinct operations are possible, leading to 8 states.
        if (n >= 3) {
            if (presses == 1) {
                return 4;
            }
            if (presses == 2) {
                return 7;
            }
            return 8;
        }

        // This part should not be reached due to constraints n>=1
        return 0; 
    }
}

Bulb Switcher II in C++

class Solution {
public:
    int flipLights(int n, int presses) {
        // If no presses are made, all bulbs remain on. Only 1 state.
        if (presses == 0) {
            return 1;
        }

        // For n=1, there are only two states: ON or OFF.
        // With 1 press, we can turn it OFF (b1, b3, b4) or leave it ON (b2).
        // With more presses, both ON and OFF states are achievable.
        if (n == 1) {
            return 2;
        }

        // For n=2, there are 4 possible states: (ON,ON), (ON,OFF), (OFF,ON), (OFF,OFF).
        // With 1 press: we can get (OFF,OFF) by b1, (ON,OFF) by b2, (OFF,ON) by b3 or b4. Total 3 states.
        // With 2 or more presses, all 4 states are achievable.
        if (n == 2) {
            if (presses == 1) {
                return 3;
            }
            return 4;
        }

        // For n>=3, the number of states saturates.
        // With 1 press: 4 distinct operations (b1, b2, b3, b4) lead to 4 states.
        // With 2 presses: 7 distinct operations are possible, leading to 7 states.
        // With 3 or more presses: all 8 distinct operations are possible, leading to 8 states.
        if (n >= 3) {
            if (presses == 1) {
                return 4;
            }
            if (presses == 2) {
                return 7;
            }
            return 8;
        }

        // This part should not be reached due to constraints n>=1
        return 0; 
    }
};

Bulb Switcher II in TypeScript

function flipLights(n: number, presses: number): number {
    // If no presses are made, all bulbs remain on. Only 1 state.
    if (presses === 0) {
        return 1;
    }

    // For n=1, there are only two states: ON or OFF.
    // With 1 press, we can turn it OFF (b1, b3, b4) or leave it ON (b2).
    // With more presses, both ON and OFF states are achievable.
    if (n === 1) {
        return 2;
    }

    // For n=2, there are 4 possible states: (ON,ON), (ON,OFF), (OFF,ON), (OFF,OFF).
    // With 1 press: we can get (OFF,OFF) by b1, (ON,OFF) by b2, (OFF,ON) by b3 or b4. Total 3 states.
    // With 2 or more presses, all 4 states are achievable.
    if (n === 2) {
        if (presses === 1) {
            return 3;
        }
        return 4;
    }

    // For n>=3, the number of states saturates.
    // With 1 press: 4 distinct operations (b1, b2, b3, b4) lead to 4 states.
    // With 2 presses: 7 distinct operations are possible, leading to 7 states.
    // With 3 or more presses: all 8 distinct operations are possible, leading to 8 states.
    if (n >= 3) {
        if (presses === 1) {
            return 4;
        }
        if (presses === 2) {
            return 7;
        }
        return 8;
    }

    // This part should not be reached due to constraints n>=1
    return 0;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.