★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-akrjrsps-ma.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
You are driving a vehicle that has capacity
empty seats initially available for passengers. The vehicle onlydrives east (ie. it cannot turn around and drive west.)node
Given a list of trips
, trip[i] = [num_passengers, start_location, end_location]
contains information about the i
-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off. The locations are given as the number of kilometers due east from your vehicle's initial location.git
Return true
if and only if it is possible to pick up and drop off all passengers for all the given trips. github
Example 1:微信
Input: trips = [[2,1,5],[3,3,7]], capacity = 4 Output: false
Example 2:app
Input: trips = [[2,1,5],[3,3,7]], capacity = 5 Output: true
Example 3:spa
Input: trips = [[2,1,5],[3,5,7]], capacity = 3 Output: true
Example 4:code
Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11 Output: true
Constraints:orm
trips.length <= 1000
trips[i].length == 3
1 <= trips[i][0] <= 100
0 <= trips[i][1] < trips[i][2] <= 1000
1 <= capacity <= 100000
假設你是一位順風車司機,車上最初有 capacity
個空座位能夠用來載客。因爲道路的限制,車 只能 向一個方向行駛(也就是說,不容許掉頭或改變方向,你能夠將其想象爲一個向量)。htm
這兒有一份行程計劃表 trips[][]
,其中 trips[i] = [num_passengers, start_location, end_location]
包含了你的第 i
次行程信息:
這些給出的地點位置是從你的 初始 出發位置向前行駛到這些地點所需的距離(它們必定在你的行駛方向上)。
請你根據給出的行程計劃表和車子的座位數,來判斷你的車是否能夠順利完成接送所用乘客的任務(當且僅當你能夠在全部給定的行程中接送全部乘客時,返回 true
,不然請返回 false
)。
示例 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
提示:
trips.length <= 1000
trips[i].length == 3
1 <= trips[i][0] <= 100
0 <= trips[i][1] < trips[i][2] <= 1000
1 <= capacity <= 100000
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 }
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 }