Skip to content

Commit 3165a79

Browse files
committed
Sync LeetCode submission Runtime - 4 ms (96.59%), Memory - 18.1 MB (98.29%)
1 parent ca0d691 commit 3165a79

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>There are <code>n</code> cars on an infinitely long road. The cars are numbered from <code>0</code> to <code>n - 1</code> from left to right and each car is present at a <strong>unique</strong> point.</p>
2+
3+
<p>You are given a <strong>0-indexed</strong> string <code>directions</code> of length <code>n</code>. <code>directions[i]</code> can be either <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, or <code>&#39;S&#39;</code> denoting whether the <code>i<sup>th</sup></code> car is moving towards the <strong>left</strong>, towards the <strong>right</strong>, or <strong>staying</strong> at its current point respectively. Each moving car has the <strong>same speed</strong>.</p>
4+
5+
<p>The number of collisions can be calculated as follows:</p>
6+
7+
<ul>
8+
<li>When two cars moving in <strong>opposite</strong> directions collide with each other, the number of collisions increases by <code>2</code>.</li>
9+
<li>When a moving car collides with a stationary car, the number of collisions increases by <code>1</code>.</li>
10+
</ul>
11+
12+
<p>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.</p>
13+
14+
<p>Return <em>the <strong>total number of collisions</strong> that will happen on the road</em>.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> directions = &quot;RLRSLL&quot;
21+
<strong>Output:</strong> 5
22+
<strong>Explanation:</strong>
23+
The collisions that will happen on the road are:
24+
- Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2.
25+
- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
26+
- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
27+
- Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 = 5.
28+
Thus, the total number of collisions that will happen on the road is 5.
29+
</pre>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> directions = &quot;LLRR&quot;
35+
<strong>Output:</strong> 0
36+
<strong>Explanation:</strong>
37+
No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= directions.length &lt;= 10<sup>5</sup></code></li>
44+
<li><code>directions[i]</code> is either <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, or <code>&#39;S&#39;</code>.</li>
45+
</ul>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Approach 2: Counting
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def countCollisions(self, directions: str) -> int:
8+
dirs = directions.lstrip('L').rstrip('R')
9+
return len(dirs) - dirs.count('S')

0 commit comments

Comments
 (0)