• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode672.灯泡开关Ⅱ|BulbSwitcherII

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10496919.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

There is a room with n lights which are turned on initially and 4 buttons on the wall. After performing exactly munknown 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:

  1. Flip all the lights.
  2. Flip lights with even numbers.
  3. Flip lights with odd numbers.
  4. 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 个按钮的功能如下:

  1. 将所有灯泡的状态反转(即开变为关,关变为开)
  2. 将编号为偶数的灯泡的状态反转
  3. 将编号为奇数的灯泡的状态反转
  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].


Runtime: 4 ms
Memory Usage: 18.9 MB
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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TIOBE 10 月编程语言排行榜:Swift 正过时?发布时间:2022-07-13
下一篇:
[Swift]LeetCode61.旋转链表|RotateList发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap