Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 67bb9f9

Browse files
committed
Merge pull request #397 from 0xff-dev/134
Add solution and test-cases for problem 134
2 parents 19a838e + 87abab3 commit 67bb9f9

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [134. Gas Station][title]
2+
3+
## Description
4+
There are `n` gas stations along a circular route, where the amount of gas at the i<sup>th</sup> station is `gas[i]`.
5+
6+
You have a car with an unlimited gas tank and it costs `cost[i]` of gas to travel from the i<sup>th</sup> station to its next (i + 1)<sup>th</sup> station. You begin the journey with an empty tank at one of the gas stations.
7+
8+
Given two integer arrays `gas` and `cost`, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return `-1`. If there exists a solution, it is **guaranteed** to be **unique**
9+
10+
**Example 1:**
11+
12+
```
13+
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
14+
Output: 3
15+
Explanation:
16+
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
17+
Travel to station 4. Your tank = 4 - 1 + 5 = 8
18+
Travel to station 0. Your tank = 8 - 2 + 1 = 7
19+
Travel to station 1. Your tank = 7 - 3 + 2 = 6
20+
Travel to station 2. Your tank = 6 - 4 + 3 = 5
21+
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
22+
Therefore, return 3 as the starting index.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: gas = [2,3,4], cost = [3,4,3]
29+
Output: -1
30+
Explanation:
31+
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
32+
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
33+
Travel to station 0. Your tank = 4 - 3 + 2 = 3
34+
Travel to station 1. Your tank = 3 - 3 + 3 = 3
35+
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
36+
Therefore, you can't travel around the circuit once no matter where you start.
37+
```
38+
39+
## 结语
40+
41+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
42+
43+
[title]: https://leetcode.com/problems/gas-station/
44+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(gas []int, cost []int) int {
4+
l := len(gas)
5+
tank, total, target := 0, 0, 0
6+
for i := 0; i < l; i++ {
7+
tank += gas[i] - cost[i]
8+
if tank < 0 {
9+
target = i + 1
10+
total += tank
11+
tank = 0
12+
}
13+
}
14+
15+
if tank+total < 0 {
16+
return -1
17+
}
18+
return target
519
}

‎leetcode/101-200/0134.Gas-Station/Solution_test.go‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputsbool
14-
expect bool
12+
name string
13+
gas, cost []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 3, 4, 5}, []int{3, 4, 5, 1, 2}, 3},
17+
{"TestCase2", []int{2, 3, 4}, []int{3, 4, 3}, -1},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.gas, c.cost)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.gas, c.cost)
2827
}
2928
})
3029
}
3130
}
3231

33-
//压力测试
32+
//压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
//使用案列
36+
//使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /