-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgas_station.cpp
More file actions
56 lines (39 loc) · 1.36 KB
/
gas_station.cpp
File metadata and controls
56 lines (39 loc) · 1.36 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
using namespace std;
/*
Let len = gas.size();
Obviously, we need to find i, so that:
for (int m = 0; m < len; ++m):
gas[ (i+m) % len] - cost[ (i+m) % len] >= 0 holds for all m
So, it's clear that
1. when sum(gas) >= sum(cost), the i exists
proof: for any i in [0, len),
gas[(i+0)%len] + gas[(i+1)%len]... + gas[(i+len-1)%len] = sum(gas)
>= sum(cost) = cost[(i+0)%len] + cost[(i+1)%len]... + cost[(i+len-1)%len]
which means
(gas[(i+0)%len] - cost[(i+0)%len]) + (gas[(i+1)%len] - cost[(i+1)%len]) + ......
+ (gas[(i+len-1)%len] - cost[(i+len-1)%len]) >= 0
*/
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost ) {
int n = gas.size();
int total_gas = 0, total_cost = 0, current_gas = 0;
int res = 0;
for (int i =0; i <n; ++i) {
total_gas += gas[i];
total_cost += cost[i];
current_gas += gas[i];
current_gas -= cost[i];
if (current_gas < 0) {
res = i+1;
current_gas = 0;
}
}
if (total_cost > total_gas) {
return -1;
}
return res;
}
};