Skip to content

Commit 95383db

Browse files
committed
Time: 101 ms (65.28%), Space: 69.4 MB (55.78%) - LeetHub
1 parent c823380 commit 95383db

File tree

1 file changed

+21
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)