返回介绍

solution / 2200-2299 / 2211.Count Collisions on a Road / README_EN

发布于 2024-06-17 01:03:08 字数 4900 浏览 0 评论 0 收藏 0

2211. Count Collisions on a Road

中文文档

Description

There are n cars on an infinitely long road. The cars are 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 ith car is moving towards the left, towards the right, or staying at its current point respectively. Each moving car has the same speed.

The number of collisions can be calculated as follows:

  • When two cars moving in opposite directions collide with each other, the number of collisions increases by 2.
  • When a moving car collides with a stationary car, the number of collisions increases by 1.

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.

Return _the total number of collisions that will happen on the road_.

 

Example 1:

Input: directions = "RLRSLL"
Output: 5
Explanation:
The collisions that will happen on the road are:
- Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2.
- 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. 

Example 2:

Input: directions = "LLRR"
Output: 0
Explanation:
No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.

 

Constraints:

  • 1 <= directions.length <= 105
  • directions[i] is either 'L', 'R', or 'S'.

Solutions

Solution 1

class Solution:
  def countCollisions(self, directions: str) -> int:
    d = directions.lstrip('L').rstrip('R')
    return len(d) - d.count('S')
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') {
      ++l;
    }
    while (r >= 0 && ds[r] == 'R') {
      --r;
    }
    int ans = 0;
    for (int i = l; i <= r; ++i) {
      if (ds[i] != 'S') {
        ++ans;
      }
    }
    return ans;
  }
}
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--;
    }
    for (int i = l; i <= r; i++) {
      count += directions[i] != 'S';
    }
    return count;
  }
};
func countCollisions(directions string) int {
  d := strings.TrimLeft(directions, "L")
  d = strings.TrimRight(d, "R")
  return len(d) - strings.Count(d, "S")
}
function countCollisions(directions: string): number {
  const n = directions.length;
  let l = 0,
    r = n - 1;
  while (l < n && directions[l] == 'L') {
    ++l;
  }
  while (r >= 0 && directions[r] == 'R') {
    --r;
  }
  let ans = 0;
  for (let i = l; i <= r; ++i) {
    if (directions[i] != 'S') {
      ++ans;
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文