[Swift]LeetCode672. 灯泡开关 Ⅱ | Bulb Switcher II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10496919.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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].
现有一个房间,墙上挂有 n
只已经打开的灯泡和 4 个按钮。在进行了 m
次未知操作后,你需要返回这 n
只灯泡可能有多少种不同的状态。
假设这 n
只灯泡被编号为 [1, 2, 3 ..., n],这 4 个按钮的功能如下:
- 将所有灯泡的状态反转(即开变为关,关变为开)
- 将编号为偶数的灯泡的状态反转
- 将编号为奇数的灯泡的状态反转
- 将编号为
3k+1
的灯泡的状态反转(k = 0, 1, 2, ...)
示例 1:
输入: n = 1, m = 1. 输出: 2 说明: 状态为: [开], [关]
示例 2:
输入: n = 2, m = 1. 输出: 3 说明: 状态为: [开, 关], [关, 开], [关, 关]
示例 3:
输入: n = 3, m = 1. 输出: 4 说明: 状态为: [关, 开, 关], [开, 关, 开], [关, 关, 关], [关, 开, 开].
注意: n
和 m
都属于 [0, 1000].
1 class Solution { 2 func flipLights(_ n: Int, _ m: Int) -> Int { 3 var n = min(n, 3) 4 return min(1 << n, 1 + m * n) 5 } 6 }
4ms
1 class Solution { 2 struct State: Hashable, OptionSet { 3 let rawValue: Int 4 5 static let oddSensitive = State(rawValue: 1) 6 static let evenInsensitive = State(rawValue: 2) 7 static let oddInsensitive = State(rawValue: 4) 8 static let evenSensitive = State(rawValue: 8) 9 10 static let odd: State = [.oddSensitive, .oddInsensitive] 11 static let even: State = [.evenSensitive, .evenInsensitive] 12 static let sensitive: State = [.evenSensitive, .oddSensitive] 13 14 static let all: State = [.oddSensitive, .oddInsensitive, .evenSensitive, .evenInsensitive] 15 } 16 17 func flipLights(_ n: Int, _ m: Int) -> Int { 18 let mask = State(rawValue: 1 << min(n, 4) - 1) 19 let permittedCount = Set<Int>(0...4).filter { $0 <= m && ($0 % 2) == (m % 2) } 20 21 var states = Set<State>() 22 23 for i in 0..<16 { 24 var count = 0, state = State.all 25 if (i & 1) != 0 { 26 state.formSymmetricDifference(.all) 27 count += 1 28 } 29 if (i & 2) != 0 { 30 state.formSymmetricDifference(.even) 31 count += 1 32 } 33 if (i & 4) != 0 { 34 state.formSymmetricDifference(.odd) 35 count += 1 36 } 37 if (i & 8) != 0 { 38 state.formSymmetricDifference(.sensitive) 39 count += 1 40 } 41 42 if permittedCount.contains(count) { 43 states.insert(state.intersection(mask)) 44 } 45 } 46 47 return states.count 48 } 49 }