Description
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:
Example 2:
Example 3:
Note: n and m both fit in range [0, 1000].
分析
题目的意思是:
有n个灯,我按照规则做m次开关操作,求这n个灯最后的种类数
这道题最多只有8种情况(我不知道怎么的出来的),所以很适合分情况来讨论:
- 当m和n其中有任意一个数是0时,返回1
- 当n = 1时,只有两种情况,0和1
- 当n = 2时,这时候要看m的次数,如果m = 1,那么有三种状态 00,01,10
- 当m > 1时,那么有四种状态,00,01,10,11
- 当m = 1时,此时n至少为3,那么我们有四种状态,000,010,101,011
- 当m = 2时,此时n至少为3,我们有七种状态:111,101,010,100,000,001,110
- 当m > 2时,此时n至少为3,我们有八种状态:111,101,010,100,000,001,110,011
代码
参考文献
[LeetCode] Bulb Switcher II 灯泡开关之二