在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You are driving a vehicle that has Given a list of Return Example 1: Input: trips = 4
Output: false
Example 2: Input: trips = 5
Output: true
Example 3: Input: trips = 3
Output: true
Example 4: Input: trips = 11
Output: true
Constraints:
假设你是一位顺风车司机,车上最初有 这儿有一份行程计划表
这些给出的地点位置是从你的 初始 出发位置向前行驶到这些地点所需的距离(它们一定在你的行驶方向上)。 请你根据给出的行程计划表和车子的座位数,来判断你的车是否可以顺利完成接送所用乘客的任务(当且仅当你可以在所有给定的行程中接送所有乘客时,返回 示例 1: 输入:trips = [[2,1,5],[3,3,7]], capacity = 4 输出:false 示例 2: 输入:trips = [[2,1,5],[3,3,7]], capacity = 5 输出:true 示例 3: 输入:trips = [[2,1,5],[3,5,7]], capacity = 3 输出:true 示例 4: 输入:trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11 输出:true 提示:
Runtime: 36 ms
Memory Usage: 20.9 MB
1 class Solution { 2 func carPooling(_ trips: [[Int]], _ capacity: Int) -> Bool { 3 var capacity = capacity 4 var stops:[Int] = [Int](repeating:0,count:1001) 5 for t in trips 6 { 7 stops[t[1]] += t[0] 8 stops[t[2]] -= t[0] 9 } 10 var i:Int = 0 11 while(capacity >= 0 && i < 1001) 12 { 13 capacity -= stops[i] 14 i += 1 15 } 16 return capacity >= 0 17 } 18 } 44ms
1 class Solution { 2 func carPooling(_ trips: [[Int]], _ capacity: Int) -> Bool { 3 var path = [Int].init(repeating: 0, count: 1001) 4 for t in trips { 5 path[t[1]] -= t[0] 6 path[t[2]] += t[0] 7 } 8 var c = capacity 9 for j in path { 10 c += j 11 if c < 0 { 12 return false 13 } 14 } 15 return true 16 } 17 } 60ms 1 class Solution { 2 func carPooling(_ trips: [[Int]], _ capacity: Int) -> Bool { 3 var road = Array(repeating: 0, count: 1000) 4 for trip in trips { 5 let count = trip[0] 6 let from = trip[1], to = trip[2] 7 for pos in from..<to { 8 road[pos] += count 9 if road[pos] > capacity { 10 return false 11 } 12 } 13 } 14 return true 15 } 16 } 68ms 1 class Solution { 2 struct Node: Comparable { 3 var addP: Int 4 var location: Int 5 init(_ addP:Int, _ location:Int) { 6 self.addP = addP 7 self.location = location 8 } 9 10 static func == (lhs:Node, rhs:Node) -> Bool { 11 return lhs.location == rhs.location 12 } 13 14 static func < (lhs:Node, rhs:Node) -> Bool { 15 if lhs.location == rhs.location { 16 return lhs.addP < rhs.addP 17 } else { 18 return lhs.location < rhs.location 19 } 20 } 21 } 22 func carPooling(_ trips: [[Int]], _ capacity: Int) -> Bool { 23 var nodes = [Node]() 24 for trip in trips { 25 nodes.append(Node(trip[0], trip[1])) 26 nodes.append(Node(-trip[0], trip[2])) 27 } 28 nodes.sort() 29 // print(nodes) 30 var counter = 0 31 for node in nodes { 32 counter += node.addP 33 if counter > capacity { 34 return false 35 } 36 } 37 return true 38 } 39 } 80ms 1 class Solution { 2 func carPooling(_ trips: [[Int]], _ capacity: Int) -> Bool { 3 let finalDestination = trips.sorted{ $0[2] < $1[2]}.last![2] 4 // let finalDestination = trips.last![2] 5 var capacityArr = Array(repeating: 0, count: finalDestination + 1) 6 7 for trip in trips { 8 for journey in trip[1]..<trip[2] { 9 capacityArr[journey] += trip[0] 10 if capacityArr[journey] > capacity { 11 return false 12 } 13 } 14 } 15 return true 16 } 17 }
|
请发表评论