Skip to content

Commit 3997a2e

Browse files
committed
Time: 2593 ms (7.62%), Space: 52.6 MB (18.1%) - LeetHub
1 parent 2da3555 commit 3997a2e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# time complexity: O(n)
2+
# space complexity: O(n)
3+
class Solution:
4+
def numberOfWays(self, corridor: str) -> int:
5+
MOD = 1_000_000_007
6+
cache = [[-1] * 3 for _ in range(len(corridor))]
7+
8+
def count(index, seats):
9+
if index == len(corridor):
10+
return 1 if seats == 2 else 0
11+
if cache[index][seats] != -1:
12+
return cache[index][seats]
13+
if seats == 2:
14+
if corridor[index] == "S":
15+
result = count(index + 1, 1)
16+
else:
17+
result = (count(index + 1, 0) + count(index + 1, 2)) % MOD
18+
else:
19+
if corridor[index] == "S":
20+
result = count(index + 1, seats + 1)
21+
else:
22+
result = count(index + 1, seats)
23+
cache[index][seats] = result
24+
return cache[index][seats]
25+
26+
return count(0, 0)
27+
28+
29+
corridor = "PPSPSP"
30+
print(Solution().numberOfWays(corridor))

0 commit comments

Comments
 (0)