commit 3041c7b184c3de23da68def32aeb59f7dceac0b9
parent 051865dc8bfdc3090372ac45d2d8d0cb476feafe
Author: Jack Sangdahl <jack@pngu.org>
Date: Sat, 13 Dec 2025 21:42:06 -0700
2147. number of ways to divide a long corridor
Diffstat:
2 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/num_ways_divide_corridor.c b/num_ways_divide_corridor.c
@@ -0,0 +1,36 @@
+// 2147. Number of Ways to Divide a Long Corridor
+#include "leetcode.h"
+
+/*
+ * along a long library corridor, there is a line of seats and decorative
+ * plants. you are given a 0-indexed string 'corridor' of length 'n' consisting
+ * of letters 's' and 'p' where each 's' represents a seat and each 'p'
+ * represents a plant. one room divider has already been installed to the left
+ * of index 0. and another to the right of index 'n - 1'. additional room
+ * dividers can be installed. for each position between indices 'i - 1' and 'i'
+ * at most one divider can be installed. divide the corridor into
+ * non-overlapping sections, where each section has exactly two seats with any
+ * number of plants. there may be multiple ways to perform the division. two
+ * ways are different if there is a position with a room divider installed in
+ * the first way but not the second way.
+ */
+
+int numberOfWays(char *corridor) {
+ static int mod = 1e9 + 7;
+ long ans = 1, j = 0, k = 0, n = strlen(corridor);
+ for (int i = 0; i < n; ++i) {
+ if (corridor[i] == 'S') {
+ if (++k > 2 && k % 2 == 1)
+ ans = ans * (i - j) % mod;
+ j = i;
+ }
+ }
+ return !(k % 2) && k > 0 ? ans : 0;
+}
+
+int main() {
+ char *c1 = "SSPPSPS", *c2 = "PPSPSP", *c3 = "S";
+ printf("%d\n", numberOfWays(c1)); // expect: 3
+ printf("%d\n", numberOfWays(c2)); // expect: 1
+ printf("%d\n", numberOfWays(c3)); // expect: 0
+}
diff --git a/num_ways_divide_corridor.py b/num_ways_divide_corridor.py
@@ -0,0 +1,35 @@
+# 2147. Number of Ways to Divide a Long Corridor
+
+"""
+along a long library corridor, there is a line of seats and decorative
+plants. you are given a 0-indexed string 'corridor' of length 'n' consisting
+of letters 's' and 'p' where each 's' represents a seat and each 'p'
+represents a plant. one room divider has already been installed to the left
+of index 0. and another to the right of index 'n - 1'. additional room
+dividers can be installed. for each position between indices 'i - 1' and 'i'
+at most one divider can be installed. divide the corridor into
+non-overlapping sections, where each section has exactly two seats with any
+number of plants. there may be multiple ways to perform the division. two
+ways are different if there is a position with a room divider installed in
+the first way but not the second way.
+"""
+
+
+class Solution(object):
+ def numberOfWays(self, corridor):
+ """
+ :type corridor: str
+ :rtype: int
+ """
+ a = [i for i, j in enumerate(corridor) if j == "S"]
+ ans, mod = 1, 10**9 + 7
+ for i in range(1, len(a) - 1, 2):
+ ans *= a[i + 1] - a[i]
+ return ans % mod * (len(a) % 2 == 0 and len(a) >= 2)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.numberOfWays(corridor="SSPPSPS"))
+ print(obj.numberOfWays(corridor="PPSPSP"))
+ print(obj.numberOfWays(corridor="S"))