Leetcode-1776. Car Fleet II [C++][Java]

目录

一、题目描述

二、解题思路

【C++】

【Java】


Leetcode-1776. Car Fleet IIhttps://leetcode.com/problems/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

二、解题思路

【C++】

class Solution {
public:
    vector<double> getCollisionTimes(vector<vector<int>>& cars) {
        int n = cars.size();
        vector<double> res(n, -1);
        stack<int> st;
        for (int i = n - 1; i >= 0; --i) {
            while (!st.empty()) {
                if (cars[i][1] <= cars[st.top()][1]) {
                    st.pop();
                } else {
                    double time = static_cast<double>(cars[st.top()][0] - cars[i][0]) / (cars[i][1] - cars[st.top()][1]);
                    if (res[st.top()] > 0 && time > res[st.top()]) {
                        st.pop();
                    } else {
                        res[i] = time;
                        break;
                    }
                }
            }
            st.push(i);
        }
        return res;
    }
};

【Java】

class Solution {
    public double[] getCollisionTimes(int[][] cars) {
        int n = cars.length;
        double[] res = new double[n];
        Arrays.fill(res, -1);
        Stack<Integer> st = new Stack<>();
        for (int i = n - 1; i >= 0; --i) {
            while (!st.isEmpty()) {
                if (cars[i][1] <= cars[st.peek()][1]) {
                    st.pop();
                } else {
                    double time = (double)(cars[st.peek()][0] - cars[i][0]) / (cars[i][1] - cars[st.peek()][1]);
                    if (res[st.peek()] > 0 && time > res[st.peek()]) {
                        st.pop();
                    } else {
                        res[i] = time;
                        break;
                    }
                }
            }
            st.push(i);
        }
        return res;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值