Skip to content

Commit 0b78f63

Browse files
committed
feat: add solutions to lc problem: No.0134
No.0134.Gas Station
1 parent 8ade8ef commit 0b78f63

File tree

5 files changed

+125
-27
lines changed

5 files changed

+125
-27
lines changed

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@
6262

6363
开始行驶,移动 $j$。若发现当前剩余汽油小于 $0$,说明当前 $i$ 作为起点不符合要求,我们将起点 $i$ 循环左移,并且更新剩余汽油,直至剩余汽油是非负数。
6464

65-
当行驶过的加油站数量达到 $n$ 时,结束。判断此时的剩余汽油是否非负,是则返回当前的 $i$ 作为答案;否则返回 $-1$,表示无解
65+
当行驶过的加油站数量达到 $n$ 时,结束行驶过程。若此时剩余汽油 $s$ 仍然小于 $0$,说明不存在这样的起点,返回 $-1$。否则,返回起点 $i$
6666

67-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 表示加油站的数量
67+
时间复杂度 $O(n)$,其中 $n$ 表示加油站的数量。空间复杂度 $O(1)$。
6868

6969
<!-- tabs:start -->
7070

@@ -162,6 +162,52 @@ func canCompleteCircuit(gas []int, cost []int) int {
162162
}
163163
```
164164

165+
### **TypeScript**
166+
167+
```ts
168+
function canCompleteCircuit(gas: number[], cost: number[]): number {
169+
const n = gas.length;
170+
let i = n - 1;
171+
let j = n - 1;
172+
let s = 0;
173+
let cnt = 0;
174+
while (cnt < n) {
175+
s += gas[j] - cost[j];
176+
++cnt;
177+
j = (j + 1) % n;
178+
while (s < 0 && cnt < n) {
179+
--i;
180+
s += gas[i] - cost[i];
181+
++cnt;
182+
}
183+
}
184+
return s < 0 ? -1 : i;
185+
}
186+
```
187+
188+
### **C#**
189+
190+
```cs
191+
public class Solution {
192+
public int CanCompleteCircuit(int[] gas, int[] cost) {
193+
int n = gas.Length;
194+
int i = n - 1, j = n - 1;
195+
int s = 0, cnt = 0;
196+
while (cnt < n) {
197+
s += gas[j] - cost[j];
198+
++cnt;
199+
j = (j + 1) % n;
200+
while (s < 0 && cnt < n) {
201+
--i;
202+
s += gas[i] - cost[i];
203+
++cnt;
204+
}
205+
}
206+
return s < 0 ? -1 : i;
207+
}
208+
}
209+
```
210+
165211
### **...**
166212

167213
```

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,52 @@ func canCompleteCircuit(gas []int, cost []int) int {
143143
}
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
function canCompleteCircuit(gas: number[], cost: number[]): number {
150+
const n = gas.length;
151+
let i = n - 1;
152+
let j = n - 1;
153+
let s = 0;
154+
let cnt = 0;
155+
while (cnt < n) {
156+
s += gas[j] - cost[j];
157+
++cnt;
158+
j = (j + 1) % n;
159+
while (s < 0 && cnt < n) {
160+
--i;
161+
s += gas[i] - cost[i];
162+
++cnt;
163+
}
164+
}
165+
return s < 0 ? -1 : i;
166+
}
167+
```
168+
169+
### **C#**
170+
171+
```cs
172+
public class Solution {
173+
public int CanCompleteCircuit(int[] gas, int[] cost) {
174+
int n = gas.Length;
175+
int i = n - 1, j = n - 1;
176+
int s = 0, cnt = 0;
177+
while (cnt < n) {
178+
s += gas[j] - cost[j];
179+
++cnt;
180+
j = (j + 1) % n;
181+
while (s < 0 && cnt < n) {
182+
--i;
183+
s += gas[i] - cost[i];
184+
++cnt;
185+
}
186+
}
187+
return s < 0 ? -1 : i;
188+
}
189+
}
190+
```
191+
146192
### **...**
147193

148194
```
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
public class Solution {
22
public int CanCompleteCircuit(int[] gas, int[] cost) {
3-
if (gas.Length == 0) return -1;
4-
var startIndex = 0;
5-
var i = 0;
6-
var gasLeft = 0;
7-
while (true)
8-
{
9-
gasLeft += gas[i] - cost[i];
10-
++i;
11-
if (i >= gas.Length) i = 0;
12-
if (gasLeft < 0)
13-
{
14-
if (startIndex >= i)
15-
{
16-
return -1;
17-
}
18-
startIndex = i;
19-
gasLeft = 0;
20-
}
21-
else
22-
{
23-
if (startIndex == i)
24-
{
25-
return startIndex;
26-
}
3+
int n = gas.Length;
4+
int i = n - 1, j = n - 1;
5+
int s = 0, cnt = 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;
2714
}
2815
}
16+
return s < 0 ? -1 : i;
2917
}
3018
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function canCompleteCircuit(gas: number[], cost: number[]): number {
2+
const n = gas.length;
3+
let i = n - 1;
4+
let j = n - 1;
5+
let s = 0;
6+
let cnt = 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+
}

solution/1100-1199/1187.Make Array Strictly Increasing/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
最后,如果 $f[n-1] \geq \infty$,说明无法转换为严格递增数组,返回 $-1$,否则返回 $f[n-1]$。
6161

62-
时间复杂度 $(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为 $arr1$ 的长度。
62+
时间复杂度 $(n \times (\log m + \min(m, n))$,空间复杂度 $O(n)$。其中 $n$ 为 $arr1$ 的长度。
6363

6464
<!-- tabs:start -->
6565

0 commit comments

Comments
 (0)