| 2 | #include <numeric> |
| 3 | |
| 4 | int gasStations(std::vector<int>& gas, std::vector<int>& cost) { |
| 5 | // If the total gas is less than the total cost, completing the |
| 6 | // circuit is impossible. |
| 7 | if (std::accumulate(gas.begin(), gas.end(), 0) < std::accumulate(cost.begin(), cost.end(), 0)) { |
| 8 | return -1; |
| 9 | } |
| 10 | int start = 0; |
| 11 | int tank = 0; |
| 12 | for (int i = 0; i < gas.size(); i++) { |
| 13 | tank += gas[i] - cost[i]; |
| 14 | // If our tank has negative gas, we cannot continue through the |
| 15 | // circuit from the current start point, nor from any station |
| 16 | // before or including the current station 'i'. |
| 17 | if (tank < 0) { |
| 18 | // Set the next station as the new start point and reset the |
| 19 | // tank. |
| 20 | start = i + 1; |
| 21 | tank = 0; |
| 22 | } |
| 23 | } |
| 24 | return start; |
| 25 | } |
nothing calls this directly
no outgoing calls
no test coverage detected