Gas Station
来源:互联网 发布:双十一前淘宝生意差 编辑:程序博客网 时间:2024/06/06 07:18
Notes:
每一站的代价和gas-station;
要知道,只要所有站的代价和大于0,就一定存在一条满足这样条件的路径;
那么接下来要做的就是求得初始点:
如果总代价〉=0,从序号0开始求代价和,如果代价和小于0,则不是从本站或者本站之前的某一个代价大于0的站开始(即说明从本站之前的站点开始是不合理的),必从下一站即之后的站开始,置sum = 0,重新开始计算,而且这样的站必定存在O(n)
public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int sum = 0; int total = 0; int start = 0; for (int i = 0; i < cost.length; i++) {sum += gas[i] - cost[i];total += gas[i] - cost[i];if(sum < 0){start = (i + 1)%cost.length;sum = 0;}} if(total < 0){ return -1; } else {return start;} }}
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
- temp2
- byte[]转16进制bug总结
- Android学习之常见的布局方式
- iOS开发之键盘类型UIKeyboardType
- 一次奇怪的ORA-12519 TNS:no appropriate service handler found
- Gas Station
- NYOJ 1118【蓝翔校长的难题】
- CSS 给DIV添加四周边框阴影
- Memcached源码分析之内存管理
- iOS开发 - 手势识别(UIGestureRecognizer)
- /etc/rc.d/rc与/etc/rc.d/init.d的关系介绍
- 架构之重构的12条军规(上)
- Debian Stable (wheezy 7.8 )升级到最新的stable (Jessie 8.0)
- reset.css