class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int start = 0;
int curGas = 0;
int totalGas = 0;
for (int i = 0; i < gas.size(); i++)
{
curGas += gas[i] - cost[i];
totalGas += gas[i] - cost[i];
if (curGas < 0)
{
start = i + 1;
curGas = 0;
}
}
if (totalGas < 0) return -1;
return start;
}
};
LeetCode134.加油站
最新推荐文章于 2024-07-13 23:05:26 发布