Skip to content

Commit b59542f

Browse files
Create gas_station.cpp
1 parent 44796e4 commit b59542f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

gas_station.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
4+
5+
int n = gas.size();
6+
int total_delta = 0, delta = 0;
7+
int start = 0, i = 0;
8+
9+
while(i < n) {
10+
total_delta += gas[i] - cost[i];
11+
delta += gas[i] - cost[i];
12+
13+
if(delta < 0) {
14+
delta = 0;
15+
start = i + 1;
16+
}
17+
i++;
18+
}
19+
20+
if(total_delta < 0) return -1;
21+
else return start;
22+
}
23+
};

0 commit comments

Comments
 (0)