题目描述
示例1
输入: n = 1, m = 1. 输出: 2 解释: 状态为: [开], [关]
示例2
输入: n = 2, m = 1. 输出: 3 解释: 状态为: [开, 关], [关, 开], [关, 关]
示例3
输入: n = 3, m = 1. 输出: 4 解释: 状态为: [关, 开, 关], [开, 关, 开], [关, 关, 关], [关, 开, 开].
提示
.
题解
代码
c++
classSolution { public: intflipLights(intn, intm) { if (m==0) return1; if (n==1) return2; m=min(m, 3); if (n==2) returnvector<int>{3, 4, 4}[m-1]; returnvector<int>{4, 7, 8}[m-1]; } };
python
classSolution: defflipLights(self, n: int, m: int) ->int: ifm==0: return1ifn==1: return2m=min(m, 3) ifn==2: return [3, 4, 4][m-1] return [4, 7, 8][m-1]
python(枚举)
classSolution: defflipLights(self, n, m): seen=set() forcandinitertools.product((0, 1), repeat=4): ifsum(cand) %2==m%2andsum(cand) <=m: A= [] foriinrange(min(n, 3)): light=1light^=cand[0] light^=cand[1] andi%2light^=cand[2] andi%2==0light^=cand[3] andi%3==0A.append(light) seen.add(tuple(A)) returnlen(seen)
作者简介:godweiyang,知乎同名,华东师范大学计算机系硕士在读,方向自然语言处理与深度学习。喜欢与人分享技术与知识,期待与你的进一步交流~