Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Bulb Switcher II

Posted on February 2, 2018July 26, 2020 by braindenny

Bulb Switcher II



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: math, array

There is a room with n lights which are turned on initially and 4 buttons on the wall. After performing exactly m unknown operations towards buttons, you need to return how many different kinds of status of the n lights could be.

Suppose n lights are labeled as number [1, 2, 3 …, n], function of these 4 buttons are given below:

Flip all the lights.
Flip lights with even numbers.
Flip lights with odd numbers.
Flip lights with (3k + 1) numbers, k = 0, 1, 2, …
Example 1:

Input: n = 1, m = 1.
Output: 2
Explanation: Status can be: [on], [off]

Example 2:

Input: n = 2, m = 1.
Output: 3
Explanation: Status can be: [on, off], [off, on], [off, off]

Example 3:

Input: n = 3, m = 1.
Output: 4
Explanation: Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].

Note: n and m both fit in range [0, 1000].

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution: reduce search space: minimize the operations, and the first 3 lights uniquely determine the state
## https://code.dennyzhang.com/bulb-switcher-ii
## Basic Ideas: array
##
##   If do the same operation twice, no changes will be made.
##   1 op2 1 op3 == 1 op1
##
##   The maximum state is 16 (2^4)
##
## Complexity: Time O(n), Space O(n)
class Solution:
    def flipLights(self, n: int, m: int) -> int:
        res = 0
        states = set()
        for (a, b, c) in [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
                          (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]:
            if a+b+c>m: continue
            d = (m-(a+b+c))%2
            # caculate the state: a, b, c, d
            n = min(n, 3)
            nums = [1]*n
            for i in range(n): 
                if a == 1: nums[i] = 1-nums[i]
                if b == 1 and i%2 ==1:
                    nums[i] = 1-nums[i]
                if c == 1 and i%2 == 0:
                    nums[i] = 1-nums[i]
                if d == 1 and i%3 == 0:
                    nums[i] = 1-nums[i]
            states.add(tuple(nums))
        return len(states)

  • Solution: reduce search space for minimize the operations
## https://code.dennyzhang.com/bulb-switcher-ii
## Basic Ideas: array
##
##   If do the same operation twice, no changes will be made.
##   1 op2 1 op3 == 1 op1
##
##   The maximum state is 16 (2^4)
##
## Complexity: Time O(n), Space O(n)
class Solution:
    def flipLights(self, n: int, m: int) -> int:
        res = 0
        states = set()
        for (a, b, c) in [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
                          (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]:
            if a+b+c>m: continue
            d = (m-(a+b+c))%2
            # caculate the state: a, b, c, d
            nums = [1]*n
            if a == 1:
                for i in range(n): nums[i] = 1-nums[i]
            if b == 1:
                for i in range(n):
                    if i%2 == 1: nums[i] = 1-nums[i]
            if c == 1:
                for i in range(n):
                    if i%2 == 0: nums[i] = 1-nums[i]
            if d == 1:
                for i in range(n):
                    if i%3 == 0: nums[i] = 1-nums[i]
            s = "".join([str(v) for v in nums])
            states.add(s)
        return len(states)
linkedin
github
slack

Post Views: 9
Posted in MediumTagged #array, #inspiring, #math

Post navigation

LeetCode: Bulb Switcher
LeetCode: Regular Expression Matching

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.