-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution134.cpp
More file actions
39 lines (38 loc) · 1.21 KB
/
Copy pathSolution134.cpp
File metadata and controls
39 lines (38 loc) · 1.21 KB
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
// first try: time O(n2) space O(1)
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
// if sum(gas) < sum (cost) return -1
// try start from 0
// if at some point cur < 0 try again from next point
for (int i = 0, cur = 0, j = 0; i < gas.size(); i ++){
for (j = i; j < i + gas.size(); j ++){
cur += gas[j % gas.size()] - cost[j % gas.size()]; // forget "%" --> overflow
if (cur < 0) break;
}
if (j == i + gas.size()) return i;
cur = 0;
}
return -1;
}
};
// second try: time O(n) space O(1)
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
// if sum(gas) < sum (cost) return -1
// try start from 0
// if at some point cur <= 0 start from next point
int total = 0, cur = 0, start = 0;
for (int i = 0, cur = 0, j = 0; i < gas.size(); i ++){
total += gas[i] - cost[i];
cur += gas[i] - cost[i];
if (cur < 0) {
cur = 0;
start = i + 1;
}
}
if (total < 0) return -1;
return start;
}
};