commit e2463525ffdacdfa6af3f1c7b9c29e248f695eff
parent 0b9642adb4828601c6e749b29aa553c5e6b48b25
Author: Jack Sangdahl <jack@pngu.org>
Date: Thu, 4 Dec 2025 08:53:45 +0100
2211. count collisions on a road
Diffstat:
2 files changed, 59 insertions(+), 0 deletions(-)
diff --git a/count_collisions_road.c b/count_collisions_road.c
@@ -0,0 +1,32 @@
+// 2211. Count Collisions on a Road
+#include "leetcode.h"
+
+/*
+ * thee are 'n' cars on an infinitely long road. the cars numbered from 0 to 'n
+ * - 1' from left to right and each car is present at a unique point. you are
+ * given a 0-indexed string 'directions' of length 'n'. 'directions[i]' can be
+ * either 'L', 'R', or 'S' denoting whether the i'th car is moving towards the
+ * left, right, or staying at its current point. each moving car has the same
+ * speed. after a collision, the cars involved can no longer move and will stay
+ * at the point where they collided. other than that, cars cannot change their
+ * state or direction of motion.
+ */
+
+int countCollisions(char *directions) {
+ int n = strlen(directions), left = 0, right = n - 1;
+ while (left < n && directions[left] == 'L')
+ left++;
+ while (right >= 0 && directions[right] == 'R')
+ right--;
+ int collisions = 0;
+ for (int i = left; i <= right; i++)
+ if (directions[i] != 'S')
+ collisions++;
+ return collisions;
+}
+
+int main() {
+ char *d1 = "RLRSLL", *d2 = "LLRR";
+ printf("%d\n", countCollisions(d1)); // expect: 5
+ printf("%d\n", countCollisions(d2)); // expect: 0
+}
diff --git a/count_collisions_road.py b/count_collisions_road.py
@@ -0,0 +1,27 @@
+# 2211. Count Collisions on a Road
+
+"""
+there are 'n' cars on an infinitely long road. the cars numbered from 0 to 'n
+- 1' from left to right and each car is present at a unique point. you are
+given a 0-indexed string 'directions' of length 'n'. 'directions[i]' can be
+either 'L', 'R', or 'S' denoting whether the i'th car is moving towards the
+left, right, or staying at its current point. each moving car has the same
+speed. after a collision, the cars involved can no longer move and will stay
+at the point where they collided. other than that, cars cannot change their
+state or direction of motion.
+"""
+
+
+class Solution(object):
+ def countCollisions(self, directions):
+ """
+ :type directions: str
+ :rtype: int
+ """
+ return sum(i != "S" for i in directions.lstrip("L").rstrip("R"))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.countCollisions(directions="RLRSLL"))
+ print(obj.countCollisions(directions="LLRR"))