diff --git a/solution/2200-2299/2211.Count Collisions on a Road/README.md b/solution/2200-2299/2211.Count Collisions on a Road/README.md index 96f15ee59205e..cd4ad43cc4826 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/README.md +++ b/solution/2200-2299/2211.Count Collisions on a Road/README.md @@ -74,7 +74,13 @@ tags: -### 方法一 +### 方法一:脑筋急转弯 + +根据题意,当两辆移动方向相反的车相撞时,碰撞次数加 $2$,即两辆车被撞停,答案加 $2$;当一辆移动的车和一辆静止的车相撞时,碰撞次数加 $1$,即一辆车被撞停,答案加 $1$。 + +而显然前缀的 $\textit{L}$ 和后缀的 $\textit{R}$ 是不会发生碰撞的,所以我们只需要统计中间不等于 $\textit{S}$ 的字符个数即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$ 或 $O(1)$。其中 $n$ 是字符串 $\textit{directions}$ 的长度。 @@ -83,8 +89,8 @@ tags: ```python class Solution: def countCollisions(self, directions: str) -> int: - d = directions.lstrip('L').rstrip('R') - return len(d) - d.count('S') + s = directions.lstrip("L").rstrip("R") + return len(s) - s.count("S") ``` #### Java @@ -92,21 +98,18 @@ class Solution: ```java class Solution { public int countCollisions(String directions) { - char[] ds = directions.toCharArray(); - int n = ds.length; - int l = 0; - int r = n - 1; - while (l < n && ds[l] == 'L') { + char[] s = directions.toCharArray(); + int n = s.length; + int l = 0, r = n - 1; + while (l < n && s[l] == 'L') { ++l; } - while (r >= 0 && ds[r] == 'R') { + while (r >= 0 && s[r] == 'R') { --r; } - int ans = 0; + int ans = r - l + 1; for (int i = l; i <= r; ++i) { - if (ds[i] != 'S') { - ++ans; - } + ans -= s[i] == 'S' ? 1 : 0; } return ans; } @@ -118,18 +121,16 @@ class Solution { ```cpp class Solution { public: - int countCollisions(string directions) { - int l = 0, r = directions.size() - 1, count = 0; - while (l <= r && directions[l] == 'L') { - l++; - } - while (l <= r && directions[r] == 'R') { - r--; + int countCollisions(string s) { + int n = s.size(); + int l = 0, r = n - 1; + while (l < n && s[l] == 'L') { + ++l; } - for (int i = l; i <= r; i++) { - count += directions[i] != 'S'; + while (r >= 0 && s[r] == 'R') { + --r; } - return count; + return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S'); } }; ``` @@ -138,9 +139,8 @@ public: ```go func countCollisions(directions string) int { - d := strings.TrimLeft(directions, "L") - d = strings.TrimRight(d, "R") - return len(d) - strings.Count(d, "S") + s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R") + return len(s) - strings.Count(s, "S") } ``` @@ -149,24 +149,49 @@ func countCollisions(directions string) int { ```ts function countCollisions(directions: string): number { const n = directions.length; - let l = 0, - r = n - 1; + let [l, r] = [0, n - 1]; while (l < n && directions[l] == 'L') { ++l; } while (r >= 0 && directions[r] == 'R') { --r; } - let ans = 0; + let ans = r - l + 1; for (let i = l; i <= r; ++i) { - if (directions[i] != 'S') { - ++ans; + if (directions[i] === 'S') { + --ans; } } return ans; } ``` +#### JavaScript + +```js +/** + * @param {string} directions + * @return {number} + */ +var countCollisions = function (directions) { + const n = directions.length; + let [l, r] = [0, n - 1]; + while (l < n && directions[l] == 'L') { + ++l; + } + while (r >= 0 && directions[r] == 'R') { + --r; + } + let ans = r - l + 1; + for (let i = l; i <= r; ++i) { + if (directions[i] === 'S') { + --ans; + } + } + return ans; +}; +``` + diff --git a/solution/2200-2299/2211.Count Collisions on a Road/README_EN.md b/solution/2200-2299/2211.Count Collisions on a Road/README_EN.md index 26d6baee153b7..8f537773bedfe 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/README_EN.md +++ b/solution/2200-2299/2211.Count Collisions on a Road/README_EN.md @@ -47,7 +47,7 @@ The collisions that will happen on the road are: - Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3. - Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4. - 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. -Thus, the total number of collisions that will happen on the road is 5. +Thus, the total number of collisions that will happen on the road is 5.
Example 2:
@@ -72,7 +72,13 @@ No cars will collide with each other. Thus, the total number of collisions that -### Solution 1 +### Solution 1: Brain Teaser + +According to the problem description, when two cars moving in opposite directions collide, the collision count increases by $2$, meaning both cars stop, and the answer increases by $2$. When a moving car collides with a stationary car, the collision count increases by $1$, meaning one car stops, and the answer increases by $1$. + +Obviously, the prefix $\textit{L}$ and the suffix $\textit{R}$ will not collide, so we only need to count the number of characters in the middle that are not $\textit{S}$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$ or $O(1)$. Here, $n$ is the length of the string $\textit{directions}$. @@ -81,8 +87,8 @@ No cars will collide with each other. Thus, the total number of collisions that ```python class Solution: def countCollisions(self, directions: str) -> int: - d = directions.lstrip('L').rstrip('R') - return len(d) - d.count('S') + s = directions.lstrip("L").rstrip("R") + return len(s) - s.count("S") ``` #### Java @@ -90,21 +96,18 @@ class Solution: ```java class Solution { public int countCollisions(String directions) { - char[] ds = directions.toCharArray(); - int n = ds.length; - int l = 0; - int r = n - 1; - while (l < n && ds[l] == 'L') { + char[] s = directions.toCharArray(); + int n = s.length; + int l = 0, r = n - 1; + while (l < n && s[l] == 'L') { ++l; } - while (r >= 0 && ds[r] == 'R') { + while (r >= 0 && s[r] == 'R') { --r; } - int ans = 0; + int ans = r - l + 1; for (int i = l; i <= r; ++i) { - if (ds[i] != 'S') { - ++ans; - } + ans -= s[i] == 'S' ? 1 : 0; } return ans; } @@ -116,18 +119,16 @@ class Solution { ```cpp class Solution { public: - int countCollisions(string directions) { - int l = 0, r = directions.size() - 1, count = 0; - while (l <= r && directions[l] == 'L') { - l++; - } - while (l <= r && directions[r] == 'R') { - r--; + int countCollisions(string s) { + int n = s.size(); + int l = 0, r = n - 1; + while (l < n && s[l] == 'L') { + ++l; } - for (int i = l; i <= r; i++) { - count += directions[i] != 'S'; + while (r >= 0 && s[r] == 'R') { + --r; } - return count; + return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S'); } }; ``` @@ -136,9 +137,8 @@ public: ```go func countCollisions(directions string) int { - d := strings.TrimLeft(directions, "L") - d = strings.TrimRight(d, "R") - return len(d) - strings.Count(d, "S") + s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R") + return len(s) - strings.Count(s, "S") } ``` @@ -147,24 +147,49 @@ func countCollisions(directions string) int { ```ts function countCollisions(directions: string): number { const n = directions.length; - let l = 0, - r = n - 1; + let [l, r] = [0, n - 1]; while (l < n && directions[l] == 'L') { ++l; } while (r >= 0 && directions[r] == 'R') { --r; } - let ans = 0; + let ans = r - l + 1; for (let i = l; i <= r; ++i) { - if (directions[i] != 'S') { - ++ans; + if (directions[i] === 'S') { + --ans; } } return ans; } ``` +#### JavaScript + +```js +/** + * @param {string} directions + * @return {number} + */ +var countCollisions = function (directions) { + const n = directions.length; + let [l, r] = [0, n - 1]; + while (l < n && directions[l] == 'L') { + ++l; + } + while (r >= 0 && directions[r] == 'R') { + --r; + } + let ans = r - l + 1; + for (let i = l; i <= r; ++i) { + if (directions[i] === 'S') { + --ans; + } + } + return ans; +}; +``` + diff --git a/solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp b/solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp index b630e97a169ba..0703d980d8203 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp +++ b/solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp @@ -1,16 +1,14 @@ class Solution { public: - int countCollisions(string directions) { - int l = 0, r = directions.size() - 1, count = 0; - while (l <= r && directions[l] == 'L') { - l++; + int countCollisions(string s) { + int n = s.size(); + int l = 0, r = n - 1; + while (l < n && s[l] == 'L') { + ++l; } - while (l <= r && directions[r] == 'R') { - r--; + while (r >= 0 && s[r] == 'R') { + --r; } - for (int i = l; i <= r; i++) { - count += directions[i] != 'S'; - } - return count; + return r - l + 1 - count(s.begin() + l, s.begin() + r + 1, 'S'); } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2211.Count Collisions on a Road/Solution.go b/solution/2200-2299/2211.Count Collisions on a Road/Solution.go index 86e2cc38c3fbb..8be5e80e1f08f 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/Solution.go +++ b/solution/2200-2299/2211.Count Collisions on a Road/Solution.go @@ -1,5 +1,4 @@ func countCollisions(directions string) int { - d := strings.TrimLeft(directions, "L") - d = strings.TrimRight(d, "R") - return len(d) - strings.Count(d, "S") -} \ No newline at end of file + s := strings.TrimRight(strings.TrimLeft(directions, "L"), "R") + return len(s) - strings.Count(s, "S") +} diff --git a/solution/2200-2299/2211.Count Collisions on a Road/Solution.java b/solution/2200-2299/2211.Count Collisions on a Road/Solution.java index da0ff82d8fe64..9cf07d330ea75 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/Solution.java +++ b/solution/2200-2299/2211.Count Collisions on a Road/Solution.java @@ -1,21 +1,18 @@ class Solution { public int countCollisions(String directions) { - char[] ds = directions.toCharArray(); - int n = ds.length; - int l = 0; - int r = n - 1; - while (l < n && ds[l] == 'L') { + char[] s = directions.toCharArray(); + int n = s.length; + int l = 0, r = n - 1; + while (l < n && s[l] == 'L') { ++l; } - while (r >= 0 && ds[r] == 'R') { + while (r >= 0 && s[r] == 'R') { --r; } - int ans = 0; + int ans = r - l + 1; for (int i = l; i <= r; ++i) { - if (ds[i] != 'S') { - ++ans; - } + ans -= s[i] == 'S' ? 1 : 0; } return ans; } -} \ No newline at end of file +} diff --git a/solution/2200-2299/2211.Count Collisions on a Road/Solution.js b/solution/2200-2299/2211.Count Collisions on a Road/Solution.js new file mode 100644 index 0000000000000..a14b9945ecc0f --- /dev/null +++ b/solution/2200-2299/2211.Count Collisions on a Road/Solution.js @@ -0,0 +1,21 @@ +/** + * @param {string} directions + * @return {number} + */ +var countCollisions = function (directions) { + const n = directions.length; + let [l, r] = [0, n - 1]; + while (l < n && directions[l] == 'L') { + ++l; + } + while (r >= 0 && directions[r] == 'R') { + --r; + } + let ans = r - l + 1; + for (let i = l; i <= r; ++i) { + if (directions[i] === 'S') { + --ans; + } + } + return ans; +}; diff --git a/solution/2200-2299/2211.Count Collisions on a Road/Solution.py b/solution/2200-2299/2211.Count Collisions on a Road/Solution.py index 011760f146f00..d3cba5965bcaf 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/Solution.py +++ b/solution/2200-2299/2211.Count Collisions on a Road/Solution.py @@ -1,4 +1,4 @@ class Solution: def countCollisions(self, directions: str) -> int: - d = directions.lstrip('L').rstrip('R') - return len(d) - d.count('S') + s = directions.lstrip("L").rstrip("R") + return len(s) - s.count("S") diff --git a/solution/2200-2299/2211.Count Collisions on a Road/Solution.ts b/solution/2200-2299/2211.Count Collisions on a Road/Solution.ts index ba7896ba20bab..0b3a71369c268 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/Solution.ts +++ b/solution/2200-2299/2211.Count Collisions on a Road/Solution.ts @@ -1,17 +1,16 @@ function countCollisions(directions: string): number { const n = directions.length; - let l = 0, - r = n - 1; + let [l, r] = [0, n - 1]; while (l < n && directions[l] == 'L') { ++l; } while (r >= 0 && directions[r] == 'R') { --r; } - let ans = 0; + let ans = r - l + 1; for (let i = l; i <= r; ++i) { - if (directions[i] != 'S') { - ++ans; + if (directions[i] === 'S') { + --ans; } } return ans;