Leetcode152: Gas Station
来源:互联网 发布:淘师湾算法与问题解决 编辑:程序博客网 时间:2024/06/05 02:07
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.
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 sum = 0; int total = 0; int start = 0; for(int i = 0; i < gas.size(); i++) { total += gas[i]-cost[i]; if(sum<0) { sum = gas[i]-cost[i]; start = i; } else sum += gas[i]-cost[i]; } return total<0? -1:start; }};
0 0
- Leetcode152: 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
- Caffe的Solver参数设置
- UVA 1395 Slim Span(kruskal算法)
- 08 Running Flink on YARN leveraging Tez
- 解决网络丢包问题及故障判断方法
- Gson简要使用笔记
- Leetcode152: Gas Station
- muduo库阅读(36)——Net部分:事件循环线程池EventLoopThreadPool
- Hanoi(汉诺塔)问题
- Android之android.intent.category.LAUNCHER的用途和使用
- 使用libsvm工具箱,README文档要仔细阅读
- 09 JobManager 高可用安装(HA)
- 《王爽——汇编语言》P206 实验室10第三个问题的答案
- Angular js 随手记
- 10 配置的详细说明