We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 836586b commit 729ce5aCopy full SHA for 729ce5a
Gas_Station.cc
@@ -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