Leetcode-853. Car Fleet [C++][Java]

目录

一、题目描述

二、解题思路


Leetcode-853. Car Fleethttps://leetcode.com/problems/car-fleet/description/

一、题目描述

There are n cars at given miles away from the starting mile 0, traveling to reach the mile target.

You are given two integer array position and speed, both of length n, where position[i] is the starting mile of the ith car and speed[i] is the speed of the ith car in miles per hour.

A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car.

car fleet is a car or cars driving next to each other. The speed of the car fleet is the minimum speed of any car in the fleet.

If a car catches up to a car fleet at the mile target, it will still be considered as part of the car fleet.

Return the number of car fleets that will arrive at the destination.

Example 1:

Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]

Output: 3

Explanation:

  • The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at target.
  • The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself.
  • The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.

Example 2:

Input: target = 10, position = [3], speed = [3]

Output: 1

Explanation:

There is only one car, hence there is only one fleet.

Example 3:

Input: target = 100, position = [0,2,4], speed = [4,2,1]

Output: 1

Explanation:

  • The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The car starting at 4 (speed 1) travels to 5.
  • Then, the fleet at 4 (speed 2) and the car at position 5 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.

Constraints:

  • n == position.length == speed.length
  • 1 <= n <= 10^5
  • 0 < target <= 10^6
  • 0 <= position[i] < target
  • All the values of position are unique.
  • 0 < speed[i] <= 10^6

二、解题思路

map按到终点的距离升序排列,从最后面的car开始对比。

【C++】

class Solution {
public:int carFleet(int target, vector<int>& position, vector<int>& speed) {map<int, double> m;for (int i = 0; i < position.size(); i++) {int dis = target - position[i];m[dis] = (double)dis / speed[i];}int res = 0;double cur = 0.0;for (auto& it : m) {if (it.second > cur) {cur = it.second;res++;}}return res;}
};

【Java】

class Solution {public int carFleet(int target, int[] position, int[] speed) {TreeMap<Integer, Double> map = new TreeMap<Integer, Double>();for (int i = 0; i < position.length; i++) {int dis = target - position[i];map.put(dis, (double)dis / speed[i]);}double cur = 0.0;int count = 0;for (double item : map.values()) {if (item > cur) {cur = item;count++;}}return count;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/26413.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue核心知识:动态路由实现完整方案

在Vue中实现动态路由&#xff0c;并结合后端接口和数据库表设计&#xff0c;是一个复杂的项目&#xff0c;需要多个技术栈和步骤的配合。以下将详细描述整个实现过程&#xff0c;包括数据库设计、后端接口设计、前端路由配置以及如何实现动态路由的功能。 目录 一、需求分析二…

CMU15445(2023fall) Project #4 - Concurrency Control踩坑历程

把树木磨成月亮最亮时的样子&#xff0c; 就能让它更快地滚下山坡&#xff0c; 有时会比骑马还快。 完整代码见&#xff1a; SnowLegend-star/CMU15445-2023fall: Having Conquered the Loftiest Peak, We Stand But a Step Away from Victory in This Stage. With unwavering…

天疱疮是一种慢性、严重的皮肤疾病

天疱疮是一种慢性、严重的皮肤疾病&#xff0c;对患者的日常生活带来很大的困扰。为了更好地预防天疱疮的发生&#xff0c;我们需要了解其成因及传播途径&#xff0c;并采取相应的预防措施。以下是关于天疱疮预防的小知识。 一、了解天疱疮 天疱疮是一种自身免疫性疾病&#…

神经网络代码入门解析

神经网络代码入门解析 import torch import matplotlib.pyplot as pltimport randomdef create_data(w, b, data_num): # 数据生成x torch.normal(0, 1, (data_num, len(w)))y torch.matmul(x, w) b # 矩阵相乘再加bnoise torch.normal(0, 0.01, y.shape) # 为y添加噪声…

解决android studio(ladybug版本) gradle的一些task突然消失了

今天不知道干了啥&#xff0c;AS&#xff08;ladybug版本&#xff09;右边gradle的task有些不见了&#xff0c;研究了半天解决了&#xff0c;这里记录下&#xff1a; 操作&#xff1a; File -->Settings-->Experimental--> 取消选项“Enable support for multi-vari…

软件测试之白盒测试知识总结

&#x1f345; 点击文末小卡片 &#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 概念与定义 白盒测试&#xff1a;侧重于系统或部件内部机制的测试&#xff0c;类型分为分支测试&#xff08;判定节点测试&#xff09;、路径测试、语句测试…

Unity中动态切换光照贴图的方法

关键代码&#xff1a;LightmapSettings.lightmaps lightmapDatas; LightmapData中操作三张图&#xff1a;lightmapColor,lightmapDir,以及一张ShadowMap 这里只操作前两张&#xff1a; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;public cl…

LLC谐振变换器恒压恒流双竞争闭环simulink仿真

1.模型简介 本仿真模型基于MATLAB/Simulink&#xff08;版本MATLAB 2017Ra&#xff09;软件。建议采用matlab2017 Ra及以上版本打开。&#xff08;若需要其他版本可联系代为转换&#xff09;针对全桥LLC拓扑&#xff0c;利用Matlab软件搭建模型&#xff0c;分别对轻载&#xf…

网络变压器的主要电性参数与测试方法(2)

Hqst盈盛&#xff08;华强盛&#xff09;电子导读&#xff1a;网络变压器的主要电性参数与测试方法&#xff08;2&#xff09;.. 今天我们继续来看看网络变压器的2个主要电性参数与它的测试方法&#xff1a; 1. 线圈间分布电容Cp:线圈间杂散静电容 测试条件:100KHz/0.1…

前端正则表达式完全指南:从入门到实战

文章目录 第一章&#xff1a;正则表达式基础概念1.1 什么是正则表达式1.2 正则表达式工作原理1.3 基础示例演示 第二章&#xff1a;正则表达式核心语法2.1 元字符大全表2.2 量词系统详解2.3 字符集合与排除 第三章&#xff1a;前端常用正则模式3.1 表单验证类3.1.1 邮箱验证3.1…

C++Primer学习(4.8位运算符)

4.8位运算符 位运算符作用于整数类型的运算对象&#xff0c;并把运算对象看成是二进制位的集合。位运算符提供检查和设置二进制位的功能&#xff0c;如17.2节(第640页)将要介绍的&#xff0c;一种名为bitset的标准库类型也可以表示任意大小的二进制位集合,所以位运算符同样能用…

排序算法(3):

这是我们的最后一篇排序算法了&#xff0c;也是我们的初阶数据结构的最后一篇了。 我们来看&#xff0c;我们之前已经讲完了插入排序&#xff0c;选择排序&#xff0c;交换排序&#xff0c;我们还剩下最后一个归并排序&#xff0c;我们今天就讲解归并排序&#xff0c;另外我们还…

【Java项目】基于SpringBoot的Java学习平台

【Java项目】基于SpringBoot的Java学习平台 技术简介&#xff1a;采用Java技术、SpringBoot框架、MySQL数据库等实现。系统基于B/S架构&#xff0c;前端通过浏览器与后端数据库进行信息交互&#xff0c;后端使用SpringBoot框架和MySQL数据库进行数据处理和存储&#xff0c;实现…

单例模式——c++

一个类&#xff0c;只能有1个对象 (对象在堆空间) 再次创建该对象&#xff0c;直接引用之前的对象 so构造函数不能随意调用 so构造函数私有 so对象不能构造 如何调用私有化的构造函数: 公开接口调用构造函数 调用构造函数&#xff1a;singleTon instance&#xff1b; 但…

lqb官方题单-速成刷题清单(上) - python版

预计3月5日 Wednesday 前完成 【2025年3月1日&#xff0c;记】题目太简单了&#xff0c;3月3日前完成 蓝桥杯速成刷题清单&#xff08;上&#xff09; https://www.lanqiao.cn/problems/1216/learning/?problem_list_id30&page1 替换题号1216 目录 进度题解和碎碎念1. 排…

虚拟化园区网络部署指南

《虚拟化园区网络部署指南》属于博主的“园区网”专栏&#xff0c;若想成为HCIE&#xff0c;对于园区网相关的知识需要非常了解&#xff0c;更多关于园区网的内容博主会更新在“园区网”专栏里&#xff0c;请持续关注&#xff01; 一.前言 华为CloudCampus解决方案基于智简网络…

Java数据结构第十五期:走进二叉树的奇妙世界(四)

专栏&#xff1a;Java数据结构秘籍 个人主页&#xff1a;手握风云 目录 一、二叉树OJ练习题&#xff08;续&#xff09; 1.1. 二叉树的层序遍历 1.2. 二叉树的最近公共祖先 1.3. 从前序与中序遍历序列构造二叉树 1.4. 从中序与后序遍历序列构造二叉树 1.5. 根据二叉树创建…

ISP 常见流程

1.sensor输出&#xff1a;一般为raw-OBpedestal。加pedestal避免减OB出现负值&#xff0c;同时保证信号超过ADC最小电压阈值&#xff0c;使信号落在ADC正常工作范围。 2. pedestal correction&#xff1a;移除sensor加的基底&#xff0c;确保后续处理信号起点正确。 3. Linea…

Java异常

一&#xff0c;Java异常概述 1.异常概述&#xff1a; 异常&#xff1a;在我们程序运行过程中出现的非正常情况 在开发中&#xff0c;即使我们的代码写的很完善&#xff0c;也有可能由于一些外因&#xff08;用户输入有误&#xff0c;文件被删除&#xff0c;网络问题&#xff…

Linux下的网络通信编程

在不同主机之间&#xff0c;进行进程间的通信。 1解决主机之间硬件的互通 2.解决主机之间软件的互通. 3.IP地址&#xff1a;来区分不同的主机&#xff08;软件地址&#xff09; 4.MAC地址&#xff1a;硬件地址 5.端口号&#xff1a;区分同一主机上的不同应用进程 网络协议…