Skip to content

Commit e0cc661

Browse files
authored
Create number-of-ways-to-divide-a-long-corridor.cpp
1 parent 8e59e09 commit e0cc661

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// greedy, combinatorics
5+
class Solution {
6+
public:
7+
int numberOfWays(string corridor) {
8+
static const int MOD = 1e9 + 7;
9+
int64_t result = 1;
10+
int cnt = 0;
11+
for (int i = 0, j = -1; i < size(corridor); ++i) {
12+
if (corridor[i] != 'S') {
13+
continue;
14+
}
15+
if (++cnt >= 3 && cnt % 2) {
16+
result = (result * (i - j)) % MOD;
17+
}
18+
j = i;
19+
}
20+
return cnt && cnt % 2 == 0 ? result : 0;
21+
}
22+
};

0 commit comments

Comments
 (0)