Gas Station
来源:互联网 发布:chart.js 提示文字 编辑:程序博客网 时间:2024/06/06 12:56
There are N gas stations along a circular route, where the amount of gas at station i
is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from
station i to its next station (i+1). You begin the journey with an empty tank at one of
the gas stations.
Return the starting gas station's index if you can travel around the circuit once,
otherwise return -1.
Note:
The solution is guaranteed to be unique.
gas的和必须大于等于cost,如果在i处gas和小于cost,i为起点。
public int canCompleteCircuit(int[] gas, int[] cost) {int start=0,total=0,totalDiff=0;for(int i=0;i<gas.length;i++){int dif=gas[i]-cost[i];if(totalDiff>=0)totalDiff+=dif;else{totalDiff=dif;start=i;}total+=dif;}if(total>=0)return start;elsereturn -1;}
0 0
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- 暑假第3周工作周报
- CSS hack 总结
- sdnu 1087.过河(NOIP 2005 提高组)DP状态压缩
- AngularJS初学习
- HDU 2112
- Gas Station
- Java注解之分类
- UITableView 及其传值
- I学霸官方免费教程三:Java基础之注释 关键字 标识符
- 老版本上的兼容ripple效果以及通用selecter
- 迷宫问题(栈解决)--2015年8月9日19:23:23v1.0版
- Socket套接字
- Java笔试面试题004
- GlTail.rb : 超 Geek 的可视化日志分析工具