-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0134-gas-station.cpp
39 lines (32 loc) · 949 Bytes
/
0134-gas-station.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Gas stations along circular route, return where to start to complete 1 trip
Ex. gas = [1,2,3,4,5] cost = [3,4,5,1,2] -> index 3 (station 4), tank = 4,8,7,6,5
At a start station, if total ever becomes negative won't work, try next station
Time: O(n)
Space: O(1)
*/
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
int totalGas = 0;
int totalCost = 0;
for (int i = 0; i < n; i++) {
totalGas += gas[i];
totalCost += cost[i];
}
if (totalGas < totalCost) {
return -1;
}
int total = 0;
int result = 0;
for (int i = 0; i < n; i++) {
total += gas[i] - cost[i];
if (total < 0) {
total = 0;
result = i + 1;
}
}
return result;
}
};