leetcode-134-Gas Station
来源:互联网 发布:编译安卓rr源码教程 编辑:程序博客网 时间:2024/06/02 02:08
代码:
/** * @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};
阅读全文
1 0
- LeetCode: Gas Station [134]
- LeetCode(134)Gas Station
- [LeetCode 134]Gas Station
- LeetCode 134 Gas Station
- leetcode || 134、Gas Station
- [leetcode] 134 Gas Station
- LeetCode(134) Gas Station
- 【Leetcode】Gas Station #134
- leetcode 134: Gas Station
- Gas Station - LeetCode 134
- 134 Gas Station [Leetcode]
- Leetcode #134 Gas Station
- LeetCode 134 Gas Station
- LeetCode 134 - gas station
- LeetCode 134 Gas Station
- LeetCode 134 Gas Station
- Leetcode 134 Gas Station
- LeetCode(134) Gas Station
- MyBatis框架核心之(八)Mybatis一级缓存与二级缓存以及自定义缓存
- iscsi的基本设定
- python装饰器(decorator)
- 【Java消息中间件】Java消息中间件( 第3章 JMS规范 )
- class文件结构
- leetcode-134-Gas Station
- BZOJ1141: [POI2009]Slw
- 服务端实现分页效果的几种思路
- 计算机视觉大规模爆发,6大细分领域将撑起725亿元市场
- python中GUI,Label和Button案例
- DLL调用(4):JavaCPP调用DLL
- 鸟哥的linux私房菜 学习总结----计算机概论(1)
- 兄弟连学python》》》》内建函数
- 解决Setting property 'source' to 'org.eclipse.jst.jee.server的问题