-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.cpp
74 lines (66 loc) · 1.97 KB
/
solution.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
vector<int> diff(n);
for (int i = 0; i < n; ++i) {
diff[i] = gas[i] - cost[i];
}
int res = -1;
int curPos = 0;
int curSum = 0;
int i = 0;
bool traversed = false;
while (!traversed) {
curPos = i;
bool cnt = true;
bool possible = true;
// while either the first element starting from curPos
// or not come back to it as end position
while (cnt || curPos != i) {
// first position
if (cnt) {
cnt = !cnt; // set cnt to false
curSum = diff[curPos];
} else {
curSum += diff[curPos];
}
// if reach negative fuel, break
if (curSum < 0) {
curSum = 0;
possible = false;
break;
}
curPos = (curPos + 1) % n;
// hit 0 atleast once, set traversed to true
if (curPos == 0) {
traversed = true;
}
}
if (possible) {
res = i;
break;
} else {
// start from nextPosition in next iteration
i = (curPos + 1) % n;
// if hit 0, set traversed to true
if (i == 0) {
traversed = true;
}
}
}
return res;
}
};
int main () {
Solution solver;
vector<int> gas = {5, 3, 2, 4, 2};
vector<int> cost = {2, 6, 4, 1, 3};
cout << solver.canCompleteCircuit(gas, cost) << endl; // 3
gas = {5, 3, 2, 4, 1};
cout << solver.canCompleteCircuit(gas, cost) << endl; // -1
return 0;
}