-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1094.拼车.go
72 lines (68 loc) · 1.46 KB
/
1094.拼车.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @lc app=leetcode.cn id=1094 lang=golang
*
* [1094] 拼车
*
* https://leetcode-cn.com/problems/car-pooling/description/
*
* algorithms
* Medium (55.85%)
* Likes: 164
* Dislikes: 0
* Total Accepted: 43.8K
* Total Submissions: 78.7K
* Testcase Example: '[[2,1,5],[3,3,7]]\n4'
*
* 车上最初有 capacity 个空座位。车 只能 向一个方向行驶(也就是说,不允许掉头或改变方向)
*
* 给定整数 capacity 和一个数组 trips , trip[i] = [numPassengersi, fromi, toi] 表示第 i
* 次旅行有 numPassengersi 乘客,接他们和放他们的位置分别是 fromi 和 toi 。这些位置是从汽车的初始位置向东的公里数。
*
* 当且仅当你可以在所有给定的行程中接送所有乘客时,返回 true,否则请返回 false。
*
*
*
* 示例 1:
*
*
* 输入:trips = [[2,1,5],[3,3,7]], capacity = 4
* 输出:false
*
*
* 示例 2:
*
*
* 输入:trips = [[2,1,5],[3,3,7]], capacity = 5
* 输出:true
*
*
*
*
* 提示:
*
*
* 1 <= trips.length <= 1000
* trips[i].length == 3
* 1 <= numPassengersi <= 100
* 0 <= fromi < toi <= 1000
* 1 <= capacity <= 10^5
*
*
*/
// @lc code=start
func carPooling(trips [][]int, capacity int) bool {
delta := make([]int, 1001)
for _, v := range trips {
delta[v[1]] += v[0]
delta[v[2]] -= v[0]
}
cur := 0
for _, v := range delta {
cur += v
if cur > capacity {
return false
}
}
return true
}
// @lc code=end