Skip to content

Commit a9acc15

Browse files
committed
feat: 1094
1 parent 7e847af commit a9acc15

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [1094. Car Pooling](https://leetcode.com/problems/car-pooling/)
2+
3+
## 2019/08/18
4+
5+
### 题目 💗[medium]
6+
7+
You are driving a vehicle that has capacity empty seats initially available for passengers. The vehicle only drives east (ie. it cannot turn around and drive west.)
8+
9+
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.
10+
11+
Return true if and only if it is possible to pick up and drop off all passengers for all the given trips.
12+
13+
Example 1:
14+
15+
```bash
16+
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
17+
Output: false
18+
```
19+
20+
Example 2:
21+
22+
```bash
23+
Input: trips = [[2,1,5],[3,3,7]], capacity = 5
24+
Output: true
25+
```
26+
27+
Example 3:
28+
29+
```bash
30+
Input: trips = [[2,1,5],[3,5,7]], capacity = 3
31+
Output: true
32+
```
33+
34+
Example 4:
35+
36+
```bash
37+
Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
38+
Output: true
39+
```
40+
41+
Constraints:
42+
43+
- trips.length <= 1000
44+
- trips[i].length == 3
45+
- 1 <= trips[i][0] <= 100
46+
- 0 <= trips[i][1] < trips[i][2] <= 1000
47+
- 1 <= capacity <= 100000

algorithm/1094.CarPooling/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package carPooling
2+
3+
func carPooling(trips [][]int, capacity int) bool {
4+
for i, max := 0, 1; i < max; i++ {
5+
num := 0
6+
for _, trip := range trips {
7+
if trip[1] <= i && trip[2] > i {
8+
num += trip[0]
9+
}
10+
if max < trip[2] {
11+
max = trip[2]
12+
}
13+
}
14+
if capacity < num {
15+
return false
16+
}
17+
}
18+
return true
19+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package carPooling
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var tcs = []struct {
10+
N1 [][]int
11+
N2 int
12+
ans bool
13+
}{
14+
// {
15+
// [][]int{
16+
// []int{2, 1, 5},
17+
// []int{3, 3, 7},
18+
// },
19+
// 4,
20+
// false,
21+
// },
22+
// {
23+
// [][]int{
24+
// []int{7, 5, 6},
25+
// []int{6, 7, 8},
26+
// []int{10, 1, 6},
27+
// },
28+
// 16,
29+
// false,
30+
// },
31+
{
32+
[][]int{
33+
[]int{2, 1, 5},
34+
[]int{3, 5, 7},
35+
},
36+
3,
37+
true,
38+
},
39+
}
40+
41+
func Test_bitwiseComplement(t *testing.T) {
42+
ast := assert.New(t)
43+
for _, tc := range tcs {
44+
ast.Equal(tc.ans, carPooling(tc.N1, tc.N2), "输入:%v", tc)
45+
}
46+
}

0 commit comments

Comments
 (0)