Skip to content

Commit 566fa82

Browse files
committed
feat: add solutions to lc problem: No.0134
No.0134.Gas Station
1 parent f470e1f commit 566fa82

File tree

6 files changed

+232
-13
lines changed

6 files changed

+232
-13
lines changed

solution/0100-0199/0134.Gas Station/README.md

+89-1
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,110 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:从任意起点开始遍历**
60+
61+
我们用 $i$, $j$ 分别标记起点和终点,用 $s$ 表示当前剩余汽油,而 $cnt$ 表示当前行驶过的加油站数量。初始时,我们将起点设在最后一个位置,即 $i=n-1$。
62+
63+
开始行驶,移动 $j$。若发现当前剩余汽油小于 $0$,说明当前 $i$ 作为起点不符合要求,我们将起点 $i$ 循环左移,并且更新剩余汽油,直至剩余汽油是非负数。
64+
65+
当行驶过的加油站数量达到 $n$ 时,结束。判断此时的剩余汽油是否非负,是则返回当前的 $i$ 作为答案;否则返回 $-1$,表示无解。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 表示加油站的数量。
68+
5969
<!-- tabs:start -->
6070

6171
### **Python3**
6272

6373
<!-- 这里可写当前语言的特殊实现逻辑 -->
6474

6575
```python
66-
76+
class Solution:
77+
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
78+
n = len(gas)
79+
i = j = n - 1
80+
cnt = s = 0
81+
while cnt < n:
82+
s += gas[j] - cost[j]
83+
cnt += 1
84+
j = (j + 1) % n
85+
while s < 0 and cnt < n:
86+
i -= 1
87+
s += gas[i] - cost[i]
88+
cnt += 1
89+
return -1 if s < 0 else i
6790
```
6891

6992
### **Java**
7093

7194
<!-- 这里可写当前语言的特殊实现逻辑 -->
7295

7396
```java
97+
class Solution {
98+
public int canCompleteCircuit(int[] gas, int[] cost) {
99+
int n = gas.length;
100+
int i = n - 1, j = n - 1;
101+
int cnt = 0, s = 0;
102+
while (cnt < n) {
103+
s += gas[j] - cost[j];
104+
++cnt;
105+
j = (j + 1) % n;
106+
while (s < 0 && cnt < n) {
107+
--i;
108+
s += gas[i] - cost[i];
109+
++cnt;
110+
}
111+
}
112+
return s < 0 ? -1 : i;
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
123+
int n = gas.size();
124+
int i = n - 1, j = n - 1;
125+
int cnt = 0, s = 0;
126+
while (cnt < n) {
127+
s += gas[j] - cost[j];
128+
++cnt;
129+
j = (j + 1) % n;
130+
while (s < 0 && cnt < n) {
131+
--i;
132+
s += gas[i] - cost[i];
133+
++cnt;
134+
}
135+
}
136+
return s < 0 ? -1 : i;
137+
}
138+
};
139+
```
74140
141+
### **Go**
142+
143+
```go
144+
func canCompleteCircuit(gas []int, cost []int) int {
145+
n := len(gas)
146+
i, j := n - 1, n - 1
147+
cnt, s := 0, 0
148+
for cnt < n {
149+
s += gas[j] - cost[j]
150+
cnt++
151+
j = (j + 1) % n
152+
for s < 0 && cnt < n {
153+
i--
154+
s += gas[i] - cost[i]
155+
cnt++
156+
}
157+
}
158+
if s < 0 {
159+
return -1
160+
}
161+
return i
162+
}
75163
```
76164

77165
### **...**

solution/0100-0199/0134.Gas Station/README_EN.md

+79-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,91 @@ Therefore, you can&#39;t travel around the circuit once no matter where you star
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
61+
n = len(gas)
62+
i = j = n - 1
63+
cnt = s = 0
64+
while cnt < n:
65+
s += gas[j] - cost[j]
66+
cnt += 1
67+
j = (j + 1) % n
68+
while s < 0 and cnt < n:
69+
i -= 1
70+
s += gas[i] - cost[i]
71+
cnt += 1
72+
return -1 if s < 0 else i
6073
```
6174

6275
### **Java**
6376

6477
```java
78+
class Solution {
79+
public int canCompleteCircuit(int[] gas, int[] cost) {
80+
int n = gas.length;
81+
int i = n - 1, j = n - 1;
82+
int cnt = 0, s = 0;
83+
while (cnt < n) {
84+
s += gas[j] - cost[j];
85+
++cnt;
86+
j = (j + 1) % n;
87+
while (s < 0 && cnt < n) {
88+
--i;
89+
s += gas[i] - cost[i];
90+
++cnt;
91+
}
92+
}
93+
return s < 0 ? -1 : i;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
104+
int n = gas.size();
105+
int i = n - 1, j = n - 1;
106+
int cnt = 0, s = 0;
107+
while (cnt < n) {
108+
s += gas[j] - cost[j];
109+
++cnt;
110+
j = (j + 1) % n;
111+
while (s < 0 && cnt < n) {
112+
--i;
113+
s += gas[i] - cost[i];
114+
++cnt;
115+
}
116+
}
117+
return s < 0 ? -1 : i;
118+
}
119+
};
120+
```
65121
122+
### **Go**
123+
124+
```go
125+
func canCompleteCircuit(gas []int, cost []int) int {
126+
n := len(gas)
127+
i, j := n - 1, n - 1
128+
cnt, s := 0, 0
129+
for cnt < n {
130+
s += gas[j] - cost[j]
131+
cnt++
132+
j = (j + 1) % n
133+
for s < 0 && cnt < n {
134+
i--
135+
s += gas[i] - cost[i]
136+
cnt++
137+
}
138+
}
139+
if s < 0 {
140+
return -1
141+
}
142+
return i
143+
}
66144
```
67145

68146
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
4+
int n = gas.size();
5+
int i = n - 1, j = n - 1;
6+
int cnt = 0, s = 0;
7+
while (cnt < n) {
8+
s += gas[j] - cost[j];
9+
++cnt;
10+
j = (j + 1) % n;
11+
while (s < 0 && cnt < n) {
12+
--i;
13+
s += gas[i] - cost[i];
14+
++cnt;
15+
}
16+
}
17+
return s < 0 ? -1 : i;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func canCompleteCircuit(gas []int, cost []int) int {
2+
n := len(gas)
3+
i, j := n - 1, n - 1
4+
cnt, s := 0, 0
5+
for cnt < n {
6+
s += gas[j] - cost[j]
7+
cnt++
8+
j = (j + 1) % n
9+
for s < 0 && cnt < n {
10+
i--
11+
s += gas[i] - cost[i]
12+
cnt++
13+
}
14+
}
15+
if s < 0 {
16+
return -1
17+
}
18+
return i
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
class Solution {
22
public int canCompleteCircuit(int[] gas, int[] cost) {
3-
if(gas.length!=cost.length) return -1;
4-
int sum=0,total=0,index=0;
5-
for(int i=0;i<gas.length;i++){
6-
int sy = gas[i] - cost[i];
7-
sum+= sy;
8-
total+= sy;
9-
if(sum<0){
10-
index=i+1;
11-
sum=0;
3+
int n = gas.length;
4+
int i = n - 1, j = n - 1;
5+
int cnt = 0, s = 0;
6+
while (cnt < n) {
7+
s += gas[j] - cost[j];
8+
++cnt;
9+
j = (j + 1) % n;
10+
while (s < 0 && cnt < n) {
11+
--i;
12+
s += gas[i] - cost[i];
13+
++cnt;
1214
}
1315
}
14-
if(total<0) return -1;
15-
return index;
16+
return s < 0 ? -1 : i;
1617
}
1718
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
3+
n = len(gas)
4+
i = j = n - 1
5+
cnt = s = 0
6+
while cnt < n:
7+
s += gas[j] - cost[j]
8+
cnt += 1
9+
j = (j + 1) % n
10+
while s < 0 and cnt < n:
11+
i -= 1
12+
s += gas[i] - cost[i]
13+
cnt += 1
14+
return -1 if s < 0 else i

0 commit comments

Comments
 (0)