Skip to content

Commit 1323289

Browse files
committed
Add solution and test-cases for problem 1094
1 parent ca207cf commit 1323289

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

leetcode/1001-1100/1094.Car-Pooling/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [1094.Car Pooling][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is a car with `capacity` empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
5+
6+
You are given the integer `capacity` and an array `trips` where trip[i] = [numPassengers<sub>i</sub>, from<sub>i</sub>, to<sub>i</sub>] indicates that the i<sup>th</sup> trip has numPassengers<sub>i</sub> passengers and the locations to pick them up and drop them off are from<sub>i</sub> and to<sub>i</sub> respectively. The locations are given as the number of kilometers due east from the car's initial location.
7+
8+
Return `true` if it is possible to pick up and drop off all passengers for all the given trips, or `false` otherwise.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
14+
Output: false
1315
```
1416

15-
## 题意
16-
> ...
17+
**Example 2:**
1718

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Car Pooling
23-
```go
2419
```
25-
20+
Input: trips = [[2,1,5],[3,3,7]], capacity = 5
21+
Output: true
22+
```
2623

2724
## 结语
2825

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(trips [][]int, capacity int) bool {
4+
maxDistance := 0
5+
for _, trip := range trips {
6+
if trip[2] > maxDistance {
7+
maxDistance = trip[2]
8+
}
9+
}
10+
11+
distances := make([]int, maxDistance+1)
12+
for _, trip := range trips {
13+
for s := trip[1]; s < trip[2]; s++ {
14+
distances[s] += trip[0]
15+
if distances[s] > capacity {
16+
return false
17+
}
18+
}
19+
}
20+
return true
521
}

leetcode/1001-1100/1094.Car-Pooling/Solution_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,39 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs [][]int
14+
cap int
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{
18+
{2, 1, 5},
19+
{3, 3, 7},
20+
}, 4, false},
21+
{"TestCase2", [][]int{
22+
{2, 1, 5},
23+
{3, 3, 7},
24+
}, 5, true},
25+
{"TestCase3", [][]int{
26+
{1, 1, 8},
27+
{2, 2, 7},
28+
{3, 3, 6},
29+
}, 5, false},
30+
{"TestCase4", [][]int{
31+
{1, 1, 8},
32+
{2, 2, 7},
33+
{3, 3, 6},
34+
}, 6, true,
35+
},
36+
{"TestCase5", [][]int{
37+
{2, 1, 5},
38+
{3, 5, 7},
39+
}, 3, true},
1940
}
2041

2142
// 开始测试
2243
for i, c := range cases {
2344
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
45+
got := Solution(c.inputs, c.cap)
2546
if !reflect.DeepEqual(got, c.expect) {
2647
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2748
c.expect, got, c.inputs)

0 commit comments

Comments
 (0)