We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cb74351 commit b0e3255Copy full SHA for b0e3255
scripts/algorithms/G/Gas Station/Gas Station.cpp
@@ -1,15 +1,16 @@
1
+// Runtime: 110 ms (Top 71.82%) | Memory: 69.4 MB (Top 89.07%)
2
class Solution {
3
public:
4
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
5
int n = gas.size();
6
int start = -1;
-
7
+
8
int sum = 0 , gastillnow = 0;
9
for(int i = 0 ; i < 2*n ; i++){
10
if(start == i%n){
11
return i%n;
12
}
- if(gas[i%n] + gastillnow >= cost[i%n]){ // we can start from this index
13
+ if(gas[i%n] + gastillnow >= cost[i%n]){ // we can start from this index
14
if(start==-1) start = i;
15
gastillnow += gas[i%n]-cost[i%n];
16
}else if(gastillnow + gas[i%n] < cost[i%n]){ // previous start index was wrong we have to start from another
@@ -19,4 +20,4 @@ class Solution {
19
20
21
return -1;
22
-};
23
+};
0 commit comments