Skip to content

Commit 729ce5a

Browse files
committed
Create Gas_Station.cc
New problem of leetcode.
1 parent 836586b commit 729ce5a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Gas_Station.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
4+
// Note: The Solution object is instantiated only once and is reused by each test case.
5+
if (gas.size() == 0)
6+
return -1;
7+
int count = 1, ret = 0, remind_gas = gas[0], tot = 0;
8+
while (count <= gas.size()) {
9+
if (remind_gas >= cost[(ret + count - 1) % gas.size()]) {
10+
remind_gas += (gas[(ret + count) % gas.size()] - cost[(ret + count - 1) % gas.size()]);
11+
count++;
12+
} else {
13+
remind_gas = gas[(ret + count) % gas.size()];
14+
ret = (ret + count) % gas.size();
15+
count = 1;
16+
}
17+
tot++;
18+
if (tot > 2 * gas.size()) {
19+
ret = -1;
20+
break;
21+
}
22+
}
23+
return ret;
24+
}
25+
};

0 commit comments

Comments
 (0)