134. Gas Station LeetCode
来源:互联网 发布:数据统计分析公司 编辑:程序博客网 时间:2024/05/21 06:50
题意:有n个加油站,每个加油站可以加可以走gas[i]米路的油,每个加油站之间的路程是cost[i],问能不能从某个加油站出发环形走一圈。
题解:gas[i] - cost[i]得到的是剩下的油量,如果总剩余量大于0,那么肯定是可以的,只是看你该怎么选起点。有没有人觉得有点像最大连续子段和,其实是的噢,用最大连续子段和来求起点。
class Solution {public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { if(gas.size() == 0 || cost.size() == 0 || gas.size() != cost.size()) return -1; int index = 0,sum,maxsum,total; sum = maxsum = total = gas[0] - cost[0]; for(int i = 1; i < gas.size(); i++) { int d = gas[i] - cost[i]; if(sum < 0) { sum = d; index = i; } else sum += d; total += d; } if(total < 0) index = -1; return index; }};
0 0
- [LeetCode]134.Gas Station
- LeetCode 134. Gas Station
- [Leetcode] 134. Gas Station
- [leetcode] 134. Gas Station
- Leetcode-134.Gas Station
- 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
- LeetCode - 134. Gas Station
- LeetCode 134. Gas Station
- LeetCode 134. Gas Station
- [leetcode] 134. Gas Station
- leetcode-134. Gas Station
- Swift - 实现发送邮件功能
- html写个超连接通过jstl传参问题
- swift简单学习之函数(func)
- display:none与visible:hidden的区别
- View.post(Runnable) ;View.postDelay(Runnable , long)
- 134. Gas Station LeetCode
- JSON.parse()和JSON.stringify()
- GaussianSplat
- 时间项目管理-单代号、双代号网络图
- java文件大小限制及其解决方案
- UITableViewCell滑动删除及移动
- RabbitMQ课程翻译(第一课)
- JSP页面展示多个PDF文件
- 对HTTP的简单理解