您的位置:首页 > 其它

leetcode-134-Gas Station

2017-12-12 21:00 417 查看
代码:

/**
* @param {number[]} gas
* @param {number[]} cost
* @return {number}
*/
var canCompleteCircuit = function(gas, cost) {
var arr = [];
var curSum = 0;
var positiveSum = 0;
var positiveIndex = 0;
var index = gas.length - 1;
gas.forEach((item, index) => {
arr.push(item - cost[index])
});
while (index >= 0) {
if (arr[index] < 0) {
curSum = arr[index--];
while (curSum < 0 && index >= 0) {
curSum += arr[index--];
}
if (index < 0) {
if (curSum + positiveSum >= 0) {
return positiveIndex
}
else {
return -1
}
}
else {
positiveSum += curSum;
positiveIndex = index + 1;
curSum = 0;
}
}
else {
while (arr[index] >= 0 && index >= 0) {
positiveSum += arr[index];
positiveIndex = index--;
}
if (index < 0) {
return 0
}
}
}
return -1
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: