Skip to content

Commit aceeef7

Browse files
authored
Create number-of-ways-to-divide-a-long-corridor.py
1 parent e0cc661 commit aceeef7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# greedy, combinatorics
5+
class Solution(object):
6+
def numberOfWays(self, corridor):
7+
"""
8+
:type corridor: str
9+
:rtype: int
10+
"""
11+
MOD = 10**9+7
12+
result, cnt, j = 1, 0, -1
13+
for i, x in enumerate(corridor):
14+
if x != 'S':
15+
continue
16+
cnt += 1
17+
if cnt >= 3 and cnt%2:
18+
result = result*(i-j)%MOD
19+
j = i
20+
return result if cnt and cnt%2 == 0 else 0

0 commit comments

Comments
 (0)