[Swift]LeetCode853. 車隊 | Car Fleet

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-hpmriygj-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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.github

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.app

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.ide

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

How many car fleets will arrive at the destination?code

Example 1:htm

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:blog

  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 }
相關文章
相關標籤/搜索