Skip to content

Commit a4c1a78

Browse files
committed
Time: 571 ms (5.1%), Space: 108.6 MB (5.55%) - LeetHub
1 parent 9d0150d commit a4c1a78

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
4+
// tank is initially 0
5+
int tank = 0;
6+
int starting_index = 0;
7+
int total_gas = 0, total_cost = 0;
8+
for(int i = 0; i< gas.size(); i++) {
9+
total_gas += gas[i];
10+
total_cost += cost[i];
11+
tank = tank + gas[i] - cost[i];
12+
if(tank < 0) {
13+
cout << "tank < 0" << endl;
14+
starting_index = i + 1;
15+
tank = 0;
16+
}
17+
cout << starting_index << endl;
18+
}
19+
if(total_gas < total_cost) return -1;
20+
else return starting_index;
21+
}
22+
};

0 commit comments

Comments
 (0)