Gas Station LeetCode solution in C++
1class Solution {2public:3int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {4int n = gas.size();5int start = -1;67int sum = 0, gastillnow = 0;8for (int i = 0; i < 2 * n; i++) {9if (start == i % n) {10return i % n;11}12if (gas[i % n] + gastillnow >= cost[i % n]) { // we can start from this index13if (start == -1) start = i;14gastillnow += gas[i % n] - cost[i % n];15} else if (gastillnow + gas[i % n] < cost[i % n]) { // previous start index was wrong we have16// to start from another17start = -1;18gastillnow = 0;19}20}21return -1;22}23};