We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 44796e4 commit b59542fCopy full SHA for b59542f
gas_station.cpp
@@ -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