Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f05347d

Browse files
committedNov 5, 2023
docs: add problem description for Leetcode 134
1 parent f86ae96 commit f05347d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
 

‎src/medium/readme.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,57 @@ Put the code below in main.rs and run `cargo run`
10321032
println!("result: {:?}", result);
10331033
```
10341034

1035+
# 134. Gas Station
1036+
1037+
## Description
1038+
1039+
There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
1040+
1041+
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
1042+
1043+
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
1044+
1045+
## Examples
1046+
1047+
```text
1048+
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
1049+
Output: 3
1050+
1051+
Explanation:
1052+
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
1053+
Travel to station 4. Your tank = 4 - 1 + 5 = 8
1054+
Travel to station 0. Your tank = 8 - 2 + 1 = 7
1055+
Travel to station 1. Your tank = 7 - 3 + 2 = 6
1056+
Travel to station 2. Your tank = 6 - 4 + 3 = 5
1057+
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
1058+
Therefore, return 3 as the starting index.
1059+
```
1060+
1061+
```text
1062+
Input: gas = [2,3,4], cost = [3,4,3]
1063+
Output: -1
1064+
1065+
Explanation:
1066+
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
1067+
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
1068+
Travel to station 0. Your tank = 4 - 3 + 2 = 3
1069+
Travel to station 1. Your tank = 3 - 3 + 3 = 3
1070+
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
1071+
Therefore, you can't travel around the circuit once no matter where you start.
1072+
```
1073+
1074+
## How to Run in main.rs
1075+
1076+
Put the code below in main.rs and run `cargo run`
1077+
1078+
```rust
1079+
let gas = vec![1, 2, 3, 4, 5];
1080+
let cost = vec![3, 4, 5, 1, 2];
1081+
let result = leetcode::medium::gas_station::can_complete_circuit(gas, cost);
1082+
println!("result: {}", result);
1083+
```
1084+
1085+
10351086
# 138. Copy List with Random Pointer
10361087

10371088
## Description

0 commit comments

Comments
 (0)