Bulb Switcher II LeetCode solution in Python

1
# Runtime: 30 ms (Top 97.1%) | Memory: 16.43 MB (Top 14.4%)
2

3

4
class Solution:
5
def flipLights(self, n: int, m: int) -> int:
6
# Reduce n to at most 3, since any action performed more than 3 times
7
# will result in a pattern that has already been counted
8
n = min(n, 3)
9
if m == 0:
10
return 1
11
elif m == 1:
12
# For m=1, there are only 2 outcomes for n=1, 3 outcomes for n=2, and 4 outcomes for n=3
13
return [2, 3, 4][n - 1]
14
elif m == 2:
15
# For m=2, there are only 2 outcomes for n=1, 4 outcomes for n=2, and 7 outcomes for n=3
16
return [2, 4, 7][n - 1]
17
else:
18
# For m>=3, there are only 2 outcomes for n=1, 4 outcomes for n=2, and 8 outcomes for n=3
19
return [2, 4, 8][n - 1]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0