Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions solution/0100-0199/0134.Gas Station/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,26 @@ public:

```go
func canCompleteCircuit(gas []int, cost []int) int {
n := len(gas)
i, j := n-1, n-1
cnt, s := 0, 0
for cnt < n {
s += gas[j] - cost[j]
cnt++
j = (j + 1) % n
for s < 0 && cnt < n {
i--
s += gas[i] - cost[i]
cnt++
totalGas, totalCost := 0, 0
tank := 0
startStation := 0

for i := 0; i < len(gas); i++ {
totalGas += gas[i]
totalCost += cost[i]
tank += gas[i] - cost[i]

if tank < 0 {

startStation = i + 1
tank = 0
}
}
if s < 0 {

if totalGas < totalCost {
return -1
}
return i
return startStation
}
```

Expand Down
29 changes: 16 additions & 13 deletions solution/0100-0199/0134.Gas Station/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,26 @@ public:

```go
func canCompleteCircuit(gas []int, cost []int) int {
n := len(gas)
i, j := n-1, n-1
cnt, s := 0, 0
for cnt < n {
s += gas[j] - cost[j]
cnt++
j = (j + 1) % n
for s < 0 && cnt < n {
i--
s += gas[i] - cost[i]
cnt++
totalGas, totalCost := 0, 0
tank := 0
startStation := 0

for i := 0; i < len(gas); i++ {
totalGas += gas[i]
totalCost += cost[i]
tank += gas[i] - cost[i]

if tank < 0 {

startStation = i + 1
tank = 0
}
}
if s < 0 {

if totalGas < totalCost {
return -1
}
return i
return startStation
}
```

Expand Down
29 changes: 16 additions & 13 deletions solution/0100-0199/0134.Gas Station/Solution.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
func canCompleteCircuit(gas []int, cost []int) int {
n := len(gas)
i, j := n-1, n-1
cnt, s := 0, 0
for cnt < n {
s += gas[j] - cost[j]
cnt++
j = (j + 1) % n
for s < 0 && cnt < n {
i--
s += gas[i] - cost[i]
cnt++
totalGas, totalCost := 0, 0
tank := 0
startStation := 0

for i := 0; i < len(gas); i++ {
totalGas += gas[i]
totalCost += cost[i]
tank += gas[i] - cost[i]

if tank < 0 {

startStation = i + 1
tank = 0
}
}
if s < 0 {

if totalGas < totalCost {
return -1
}
return i
return startStation
}