-
Notifications
You must be signed in to change notification settings - Fork 3
/
canCompleteCircuit.cpp
57 lines (51 loc) · 1.37 KB
/
canCompleteCircuit.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
/*
lintcode: gas station
http://www.lintcode.com/zh-cn/problem/gas-station/
*/
class Solution {
public:
/**
* @param gas: a vector of integers
* @param cost: a vector of integers
* @return: an integer
*/
bool canComplete(vector<int> &gas, vector<int> &cost, int index) {
int remain = 0;
int sz = gas.size();
for(int i=index; i<index+sz; i++) {
remain += gas[i%sz];
remain -= cost[i%sz];
if(remain < 0) {
return false;
}
}
return true;
}
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
// write your code here
// pick one choice
for(int i=0; i<gas.size(); i++) {
if(gas[i] >= cost[i]) {
if(canComplete(gas, cost, i)) {
return i;
}
}
}
return -1;
}
};
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
if(gas.size() == 0 || cost.size() == 0) {
return -1;
}
int sum = 0, total = 0, index = 0;
for(int i=0; i<gas.size(); i++) {
sum += gas[i]-cost[i];
total += gas[i] - cost[i];
if(sum < 0) {
index = i+1;
sum = 0;
}
}
return total>=0?index:-1;
}