-
Notifications
You must be signed in to change notification settings - Fork 95
/
gas-station.cpp
87 lines (84 loc) · 2.36 KB
/
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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
/*************************************************
Solution 1: a bad solution, written in 2013
*************************************************/
//time: O(n), space: O(n), find smallest coninuous sub-array
class Solution {
public:
struct Range {
int l,r;
long long sum;
};
int sz;
void findSCSA(long long arr[], Range &res)
{
res.sum = (long long)0x7fffffffffffffff;
int i = 0, l = 0, sum = 0;
while(l < sz && (i - l) < sz)
{
sum += arr[i%sz];
if(sum < res.sum)
{
res.l = l;
res.r = i;
res.sum = sum;
}
if(sum > 0)
{
l = i + 1;
sum = 0;
}
++i;
}
}
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
// Note: The Solution object is instantiated only once and is reused by each test case.
sz = gas.size();
long long *ab = new long long[sz];
for(int i = 0; i < sz; i++)
ab[i] = gas[i] - cost[i];
Range res;
findSCSA(ab, res);
if(res.sum >= 0)
{
delete []ab;
return 0;
}
long long sum = 0;
for(int i = res.r + 1; (i % sz) != res.l; ++i)
{
sum += ab[i%sz];
if(sum < 0)
{
delete []ab;
return -1;
}
}
delete []ab;
if(sum < -res.sum)
return -1;
return (res.r + 1)%sz;
}
};
/*************************************************
Solution 2: a better solution, written in 2014
*************************************************/
//time: O(n), space: O(1)
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int stations = gas.size(), st = 0, ed = 0, energy = 0;//gas[0];
while(ed < st + stations) {
if(energy < 0) { //move start point backward
st -= 1;
energy += (gas[st + stations] - cost[st + stations]);
} else { //move end point forward
energy += (gas[ed] - cost[ed]);
ed += 1;
}
}
if(energy < 0)
return -1;
else
return (st + stations) % stations;
}
};