返回介绍

solution / 1700-1799 / 1776.Car Fleet II / README_EN

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

1776. Car Fleet II

中文文档

Description

There are n cars traveling at different speeds in the same direction along a one-lane road. You are given an array cars of length n, where cars[i] = [positioni, speedi] represents:

  • positioni is the distance between the ith car and the beginning of the road in meters. It is guaranteed that positioni < positioni+1.
  • speedi is the initial speed of the ith car in meters per second.

For simplicity, cars can be considered as points moving along the number line. Two cars collide when they occupy the same position. Once a car collides with another car, they unite and form a single car fleet. The cars in the formed fleet will have the same position and the same speed, which is the initial speed of the slowest car in the fleet.

Return an array answer, where answer[i] is the time, in seconds, at which the ith car collides with the next car, or -1 if the car does not collide with the next car. Answers within 10-5 of the actual answers are accepted.

 

Example 1:

Input: cars = [[1,2],[2,1],[4,3],[7,2]]
Output: [1.00000,-1.00000,3.00000,-1.00000]
Explanation: After exactly one second, the first car will collide with the second car, and form a car fleet with speed 1 m/s. After exactly 3 seconds, the third car will collide with the fourth car, and form a car fleet with speed 2 m/s.

Example 2:

Input: cars = [[3,4],[5,4],[6,3],[9,1]]
Output: [2.00000,1.00000,1.50000,-1.00000]

 

Constraints:

  • 1 <= cars.length <= 105
  • 1 <= positioni, speedi <= 106
  • positioni < positioni+1

Solutions

Solution 1

class Solution:
  def getCollisionTimes(self, cars: List[List[int]]) -> List[float]:
    stk = []
    n = len(cars)
    ans = [-1] * n
    for i in range(n - 1, -1, -1):
      while stk:
        j = stk[-1]
        if cars[i][1] > cars[j][1]:
          t = (cars[j][0] - cars[i][0]) / (cars[i][1] - cars[j][1])
          if ans[j] == -1 or t <= ans[j]:
            ans[i] = t
            break
        stk.pop()
      stk.append(i)
    return ans
class Solution {
  public double[] getCollisionTimes(int[][] cars) {
    int n = cars.length;
    double[] ans = new double[n];
    Arrays.fill(ans, -1.0);
    Deque<Integer> stk = new ArrayDeque<>();
    for (int i = n - 1; i >= 0; --i) {
      while (!stk.isEmpty()) {
        int j = stk.peek();
        if (cars[i][1] > cars[j][1]) {
          double t = (cars[j][0] - cars[i][0]) * 1.0 / (cars[i][1] - cars[j][1]);
          if (ans[j] < 0 || t <= ans[j]) {
            ans[i] = t;
            break;
          }
        }
        stk.pop();
      }
      stk.push(i);
    }
    return ans;
  }
}
class Solution {
public:
  vector<double> getCollisionTimes(vector<vector<int>>& cars) {
    int n = cars.size();
    vector<double> ans(n, -1.0);
    stack<int> stk;
    for (int i = n - 1; ~i; --i) {
      while (stk.size()) {
        int j = stk.top();
        if (cars[i][1] > cars[j][1]) {
          double t = (cars[j][0] - cars[i][0]) * 1.0 / (cars[i][1] - cars[j][1]);
          if (ans[j] < 0 || t <= ans[j]) {
            ans[i] = t;
            break;
          }
        }
        stk.pop();
      }
      stk.push(i);
    }
    return ans;
  }
};
func getCollisionTimes(cars [][]int) []float64 {
  n := len(cars)
  ans := make([]float64, n)
  for i := range ans {
    ans[i] = -1.0
  }
  stk := []int{}
  for i := n - 1; i >= 0; i-- {
    for len(stk) > 0 {
      j := stk[len(stk)-1]
      if cars[i][1] > cars[j][1] {
        t := float64(cars[j][0]-cars[i][0]) / float64(cars[i][1]-cars[j][1])
        if ans[j] < 0 || t <= ans[j] {
          ans[i] = t
          break
        }
      }
      stk = stk[:len(stk)-1]
    }
    stk = append(stk, i)
  }
  return ans
}

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

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

发布评论

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