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) Post Views: 9 Post navigation LeetCode: Bulb SwitcherLeetCode: Regular Expression Matching Leave a Reply Cancel replyYour email address will not be published.Comment Name Email Website