当前位置: 首页 > news >正文

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;
    }
}

http://www.dtcms.com/a/46684.html

相关文章:

  • 第50天:Web开发-JavaEE应用SpringBoot栈ActuatorSwaggerHeapDump提取自动化
  • 翻转--蓝桥
  • 【深入剖析:机器学习、深度学习与人工智能的关系】
  • 【漫话机器学习系列】114.逻辑 Sigmoid 函数
  • 【Kubernets】K8S内部nginx访问Service资源原理说明
  • python爬虫Scapy框架(1)
  • 精灵图又名雪碧图的使用方法
  • idea生成自定义Maven原型(archetype)项目工程模板
  • windows系统本地部署DeepSeek模型
  • 大模型 参数 use_cache 怎么用? 与 KV Cache 是什么关系?
  • Qt 的 Lambda 捕获局部变量导致 UI 更新异常的分析与解决
  • netty18罗汉——布袋罗汉(encoder)
  • 如何在Python用Plot画出一个简单的机器人模型
  • llama-factory || AutoDL平台
  • ASP.NET Core 3.1 修改个别API返回JSON序列化格式
  • MySQL整体架构
  • Docker - 网络
  • 用冒泡排序法模拟qsort函数
  • LabVIEW中三种PSD分析VI的区别与应用
  • 微调训练方法概述:Fine-tuning、Prompt-tuning、P-tuning 及其他高效技术
  • pwa的基本使用
  • 2W8000字 LLM架构文章阅读指北
  • pytorch2.6.0版本测试YOLOv5中detect.py错误解决办法
  • http报文的content-type参数和spring mvc传参问题
  • 高频 SQL 50 题(基础版)_550. 游戏玩法分析 IV
  • 系统架构设计师—计算机基础篇—系统性能评价
  • 深度学习pytorch之4种归一化方法(Normalization)原理公式解析和参数使用
  • 小结:BGP协议
  • AtCoder Beginner Contest 001(A - 積雪深差、B - 視程の通報、C - 風力観測、D - 感雨時刻の整理)题目翻译
  • 贪心算法+题目