forked from ByteByteGoHq/coding-interview-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgas_stations.cpp
More file actions
25 lines (24 loc) · 844 Bytes
/
Copy pathgas_stations.cpp
File metadata and controls
25 lines (24 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <vector>
#include <numeric>
int gasStations(std::vector<int>& gas, std::vector<int>& cost) {
// If the total gas is less than the total cost, completing the
// circuit is impossible.
if (std::accumulate(gas.begin(), gas.end(), 0) < std::accumulate(cost.begin(), cost.end(), 0)) {
return -1;
}
int start = 0;
int tank = 0;
for (int i = 0; i < gas.size(); i++) {
tank += gas[i] - cost[i];
// If our tank has negative gas, we cannot continue through the
// circuit from the current start point, nor from any station
// before or including the current station 'i'.
if (tank < 0) {
// Set the next station as the new start point and reset the
// tank.
start = i + 1;
tank = 0;
}
}
return start;
}