[Swift]LeetCode853. 车队 | Car Fleet

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10592755.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

N cars are going to the same destination along a one lane road.  The destination is target miles away.

Each car i has a constant speed speed[i] (in miles per hour), and initial position position[i] miles towards the target along the road.

A car can never pass another car ahead of it, but it can catch up to it, and drive bumper to bumper at the same speed.

The distance between these two cars is ignored - they are assumed to have the same position.

A car fleet is some non-empty set of cars driving at the same position and same speed.  Note that a single car is also a car fleet.

If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.

How many car fleets 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 and 8 become a fleet, meeting each other at 12.
The car starting at 0 doesn't catch up to any other car, so it is a fleet by itself.
The cars starting at 5 and 3 become a fleet, meeting each other at 6.
Note that no other cars meet these fleets before the destination, so the answer is 3.

Note:

  1. 0 <= N <= 10 ^ 4
  2. 0 < target <= 10 ^ 6
  3. 0 < speed[i] <= 10 ^ 6
  4. 0 <= position[i] < target
  5. All initial positions are different.

N  辆车沿着一条车道驶向位于 target 英里之外的共同目的地。

每辆车 i 以恒定的速度 speed[i] (英里/小时),从初始位置 position[i] (英里) 沿车道驶向目的地。

一辆车永远不会超过前面的另一辆车,但它可以追上去,并与前车以相同的速度紧接着行驶。

此时,我们会忽略这两辆车之间的距离,也就是说,它们被假定处于相同的位置。

车队 是一些由行驶在相同位置、具有相同速度的车组成的非空集合。注意,一辆车也可以是一个车队。

即便一辆车在目的地才赶上了一个车队,它们仍然会被视作是同一个车队。 

会有多少车队到达目的地? 

示例:

输入:target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
输出:3
解释:
从 10 和 8 开始的车会组成一个车队,它们在 12 处相遇。
从 0 处开始的车无法追上其它车,所以它自己就是一个车队。
从 5 和 3 开始的车会组成一个车队,它们在 6 处相遇。
请注意,在到达目的地之前没有其它车会遇到这些车队,所以答案是 3。

提示:

    1. 0 <= N <= 10 ^ 4
    2. 0 < target <= 10 ^ 6
    3. 0 < speed[i] <= 10 ^ 6
    4. 0 <= position[i] < target
    5. 所有车的初始位置各不相同。

 232ms

 1 class Solution {
 2 
 3     func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int {
 4         let cars = zip(position, speed).map{Car(position: $0.0, speed: $0.1)}.sorted{$0.position > $1.position}
 5 
 6         var fleets = 0
 7         var currentTime = 0.0
 8         for c in cars {
 9             let carTime = c.time(toTarget: target)
10             if carTime > currentTime {
11                 currentTime = carTime
12                 fleets += 1
13             }
14         }
15         return fleets
16     }
17 }
18 
19 struct Car {
20     let position : Int
21     let speed : Int
22     
23     func time(toTarget target: Int) -> Double {
24         return Double(target - position) / Double(speed)
25     }
26 }

240ms

 1 class Solution {
 2     func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int {
 3         guard position.count == speed.count else { return -1 }
 4         let n = position.count
 5         var cars = [Car]()
 6         for i in 0..<n {
 7             cars.append(Car(position: position[i], time: (Float)(target - position[i]) / Float(speed[i])))
 8         }
 9         cars.sort(by:{
10             $0.position < $1.position
11         })
12         var t = n - 1
13         var ret = 0
14         while (t > 0) {
15             if cars[t].time < cars[t-1].time {
16                 ret += 1
17             } else {
18                 cars[t-1].time = cars[t].time
19             }
20             t -= 1
21         }
22         return ret + (t == 0 ? 1: 0)
23     }
24     
25     struct Car {
26         let position: Int
27         var time: Float
28     }
29 }

Runtime: 248 ms
Memory Usage: 19.4 MB
 1 class Solution {
 2     func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int {
 3         var map:[Int:Double] = [Int:Double]()
 4         for i in 0..<position.count
 5         {
 6            map[target - position[i]] = Double(speed[i])   
 7         }
 8         var res:Int = 0
 9         var cur:Double = 0.0
10         var nums = Set(map.keys).sorted(by:<)
11         for key in nums
12         {
13             var time:Double = Double(key) / map[key,default:0]
14             if time > cur
15             {
16                 cur = time
17                 res += 1
18             }
19         }
20         return res
21     }
22 }

252ms

 1 class Solution {
 2     func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int {
 3         var orders: [Int: Double] = [:]
 4         for (index, pos) in position.enumerated() {
 5             let steps = Double(target - pos) / Double(speed[index])
 6             orders[pos] = steps
 7         }
 8         
 9         var stack: [Double] = []
10         for pair in orders.sorted(by: { $0.key > $1.key }) {
11             if stack.isEmpty {
12                 stack.append(pair.value)
13             } else {
14                 if stack.last! < pair.value {
15                     stack.append(pair.value)
16                 }
17             }
18         }
19         return stack.count
20     }
21 }

280ms

 1 class Solution {
 2     func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int {
 3         var times: [Int: Double] = [:]
 4         let n = position.count
 5         for i in 0..<n {
 6             let time = Double(target - position[i])/Double(speed[i])
 7             times[position[i]] = time
 8         }
 9         var res = 0
10         var prevTime = 0.0
11         for (_, time) in times.sorted(by: { $0.key > $1.key }) {
12             if time > prevTime {
13                 res += 1
14                 prevTime = time
15             }
16         }
17         return res
18     }
19 }   

288ms

 1 class Solution {
 2     func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int {
 3         var fleets = 0
 4         var max = -1.0
 5 
 6         var distribution = [Double](repeating: -1, count: target+1)
 7 
 8         for i in 0..<position.count {
 9             distribution[position[i]] = Double(target - position[i])/Double(speed[i])
10         }
11 
12         var i = distribution.count - 1
13         while i>=0 {
14             if distribution[i] > max {
15                 max = distribution[i]
16                 fleets += 1
17             }
18             i -= 1
19         }
20         return fleets
21     }
22 }

 

转载于:https://www.cnblogs.com/strengthen/p/10592755.html

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

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

相关文章

世界机器人大会|人工智能VS人类

来源&#xff1a;新华社作者&#xff1a;北京邮电大学人工智能学院 刘伟配音&#xff1a;郑琬策划、终审&#xff1a;刘君校对&#xff1a;周雪晴2021世界机器人大会于9月10日至13日在北京召开&#xff0c;世界机器人博览会及世界机器人大赛将同期举办。其实&#xff0c;智能不…

LeetCode LCP 06. 拿硬币

题目&#xff1a;桌上有 n 堆力扣币&#xff0c;每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆&#xff0c;拿走其中的一枚或者两枚&#xff0c;求拿完所有力扣币的最少次数。 示例 1&#xff1a; 输入&#xff1a;[4,2,1]输出&#xff1a;4解释&#xff1a;第一…

华为:憧憬6G,共同定义6G

来源&#xff1a;华为华为心声社区发布了由徐直军签发的总裁办电子邮件&#xff0c;邮件内容为徐直军为《6G无线通信新征程》一书作的序《憧憬6G&#xff0c;共同定义6G》。徐直军在文中表示&#xff0c;6G将在2030年左右投向市场&#xff0c;究竟市场将会迎来什么样的6G&#…

增量式爬虫(简易)

增量式爬虫引言&#xff1a; 当我们在浏览相关网页的时候会发现&#xff0c;某些网站定时会在原有网页数据的基础上更新一批数据&#xff0c;例如某电影网站会实时更新一批最近热门的电影。小说网站会根据作者创作的进度实时更新最新的章节数据等等。那么&#xff0c;类似的情景…

“中国诺奖”2021未来科学大奖公布:袁国勇、裴伟士、张杰、施敏获奖,总奖金300万美元...

来源&#xff1a;学术头条中国首个由科学家、企业家共同发起的民间公益组织颁发的世界级科学大奖——未来科学大奖&#xff0c;9 月 12 日正式揭晓 2021 年生命科学奖、物质科学奖、数学与计算机科学奖获奖名单。香港大学袁国勇、裴伟士获得生命科学奖。获奖理由&#xff1a;他…

实验二——函数重载,快速排序,类对象

函数重载&#xff1a; #include<iostream> using namespace std; struct complex{ double real; double imaginary; }; int add(int,int); double add(double,double); complex add(complex,complex); int main() { int a12,b13; double a22.0,b23.0; struct complex num…

LeetCode 771. 宝石与石头

题目&#xff1a; 给定字符串J 代表石头中宝石的类型&#xff0c;和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型&#xff0c;你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复&#xff0c;J 和 S中的所有字符都是字母。字母区分大小写&#…

Python——类与对象,异常处理

类 1 class C1: 2 def setdata(self,value): 3 self.data value 4 def display(self): 5 print(self.data) 1 class C2(C1): //继承&#xff0c;2 def display(self): 3 print(C2:, self.data)…

【前沿技术】Facebook 硬件负责人,带摄像头的智能眼镜将在 10 年内成为常态

拍照功能将在十年内成为智能眼镜的标准配置来源&#xff1a;智能研究院在 Facebook 与 Luxottica 的首款智能眼镜合作产品 Ray-Ban Stories 发布后&#xff0c;Facebook 硬件业务负责人 Andrew Bosworth 周五在与 Essilor Luxottica 的首席可穿戴设备官 Rocco Basilico 交谈时表…

LeetCode 1431. 拥有最多糖果的孩子

题目&#xff1a;给你一个数组 candies 和一个整数 extraCandies &#xff0c;其中 candies[i] 代表第 i 个孩子拥有的糖果数目。 对每一个孩子&#xff0c;检查是否存在一种方案&#xff0c;将额外的 extraCandies 个糖果分配给孩子们之后&#xff0c;此孩子有 最多 的糖果。…

#leetcode刷题之路35-搜索插入位置

给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。 示例 1:输入: [1,3,5,6], 5输出: 2示例 2:输入: [1,3,5,6], 2输出: 1示例 3:输入: …

9大领域50名青年学者获2021年科学探索奖,单人奖金300万元

来源&#xff1a;科学探索奖官网、科学网等9 月 13 日&#xff0c;2021 年科学探索奖获奖人名单公布&#xff0c;来自 9 个领域的 50 名青年科学家获奖。其中包括 8 名女性科学家&#xff0c;最年轻获奖者仅 32 岁。他们将在 5 年内获得总计 300 万元人民币的奖金&#xff0c;可…

ajax传递数组,后台接收为null解决方法

traditional:true,加上这个就好&#xff0c;默认为false,即允许深度序列化参数&#xff0c;但是servlet api不支持&#xff0c;所有设为true阻止就好了。 $.ajax({ type:post, url:/lst, async:true, data:{"arr_id":arr_id}, traditional:true, s…

多角度回顾因果推断的模型方法

来源&#xff1a;AI干货知识库推断因果关系&#xff0c;是人类思想史与科学史上的重要主题。现代因果推断的研究&#xff0c;始于约尔-辛普森悖论&#xff0c;经由鲁宾因果模型、随机试验等改进&#xff0c;到朱力亚珀尔的因果革命&#xff0c;如今因果科学与人工智能的结合正掀…

torchvision包的主要构成

torchvision包是服务于PyTorch深度学习框架的&#xff0c;主要用来构建计算机视觉模型。 torchvision 主要由以下几部分构成&#xff1a; torchvision.datasets&#xff1a;一些加载数据的函数及常用的数据集接口&#xff1b;torchvision.models&#xff1a;包含常用的模型结…

Eclipse+ADT+Android SDK 搭建安卓开发环境

要求&#xff1a;windows 7 基本操作。运行环境&#xff1a;windows 7(64位); eclipse-jee-luna-SR2-win32(32位);ADT-23.0.4 最近刚开始接触Android(安卓)嵌入式开发&#xff0c;首要问题是搭建Andoid开发环境&#xff0c;由于本人用的是windows7的笔记本&#xff0c;也就只能…

机器学习与数据挖掘简介

机器学习的目的是预测&#xff08;包括分类和回归&#xff09;。 分类是根据输入数据&#xff0c;判别这些数据隶属于哪个类别。 回归则是根据输入数据&#xff0c;计算出一个输出值。输入数据一般为一个向量&#xff0c;向量的各个分量也称为特征&#xff08;Feature&#xff…

骆利群院士最新Science综述:神经环路架构,激发新的AI

来源&#xff1a;ScienceAI编辑&#xff1a;凯霞人脑包含大约 1000 亿个神经元&#xff0c;每个神经元都有数千个突触连接。尽管单个神经元是神经系统的基本单位&#xff0c;但正是它们的突触连接模式使神经元能够为特定功能形成专门的神经环路&#xff0c;从而使大脑成为强大的…

软件设计作业 1

第一部分先列出本次采用Scrum敏捷编程的任务完成情况&#xff0c;并写出心得 酒店管理系统能够极大的方便酒店的工资人员在关于酒店的管理的操作&#xff0c;如客人入住、退房&#xff0c;信息录入、查询等&#xff0c;极大的提高了酒店整体管理活动的工作效率。 使用Scrum使得…

决策树简介与入门

决策树表示对象属性&#xff08;比如贷款用户的年龄、是否有工作、是否有房产、信用评分等&#xff09;和对象类别&#xff08;是否批准其贷款申请&#xff09;之间的一种映射。使用层层推理来实现最终的分类。  根节点&#xff1a;包含样本的全集  内部节点&#xff1a;对…