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.

1 如果总的gas - cost小于零的话,那么没有解返回-1

2 如果前面所有的gas - cost加起来小于零,那么前面所有的点都不能作为出发点。

3 选择最佳出发点后,判断从这点出发能否环行一圈。

class Solution {
public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
//vector<int> sum;
int sum=;
int res=-;
for(int i=;i<gas.size();i++)
{
int temp=gas[i]-cost[i];
sum+=temp;
if(res==-&&sum>=)
res=i;
else if(sum<)
{
sum=;
res=-;
}
}
if(res!=-)
{
for(int i=;i<res;i++)
{
int temp=gas[i]-cost[i];
sum+=temp;
if(sum<)
{
res=-;
break;
}
} }
return res;
}
};

Gas Station——又是一道经典问题的更多相关文章

  1. [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

    LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...

  2. 134. Gas Station

    题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

  3. [leetcode]134. Gas Station加油站

      There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...

  4. [LeetCode 题解]:Gas Station

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 There are ...

  5. [LeetCode] Gas Station 贪心

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  6. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  7. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  8. Leetcode 134 Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  9. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

随机推荐

  1. G. Trace ACM-ICPC 2018 徐州赛区网络预赛 线段树写法

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy  ...

  2. Desert King 最小比率生成树 (好题)

    Description David the Great has just become the king of a desert country. To win the respect of his ...

  3. centos7安装uwsgi报错

    错误为: [root@bogon ~]# pip install uwsgi Collecting uwsgi Using cached uwsgi-.tar.gz Installing collec ...

  4. 阿里C++研发实习二面和三面面经

    下午连着面了阿里爸爸的二面和三面,非常不明白别人的三面都是hr了,为什么我还是在技术面,难道面了个假阿里.不管怎么样,来篇面经攒攒人品. 二面 第一次遇到这么严肃的面试官,居然可以全程无表情的,面了这 ...

  5. 2015/9/9 Python基础(10):文件和输入输出

    文件对象文件对象不仅可以用来访问普通的磁盘文件,而且也可以访问其它任何类型抽象层面上的“文件”.一旦设置了合适的“钩子”,你就可以访问文件类型接口的其它对象,就好像访问的是普通文件一样.文件对象的处理 ...

  6. Maven-Dependency Mechanism

    依赖管理是maven的一个广为人知的特性, 这也是maven擅长的一个领域. 为单一的工程管理依赖不是很难, 但当你着手处理多模块工程和包含数十或数百个模块的应用时, maven可以帮助你很好地保持高 ...

  7. IIS7绑定多个HTTPS网站并应用自签名证书

    本文主要介绍如何在IIS中添加多个网站并使用同一个数字签名证书(win7+IIS7.5) IIS中添加站点site1,端口号为80,主机名为空.如下图: 创建证书 IIS->Server Cer ...

  8. 老生常谈-Activity(山东数漫江湖)

    对于activity的七个声生命周期回调,总是被大家翻来覆去的说,甚至说的都有些厌烦了,这部分知识虽然基础但也很重要,谁都不想在面试的时候只说出个一知半解,下面的分析是对阅读<安卓开发艺术探索& ...

  9. idea 导入 java json 包

    1.java 项目导包 找到 External Libraries 下面的java版本包,在点击鼠标右键.直接找到jar路径全部选中导入即可.

  10. bzoj 1968 数学

    在1-n所有数中,i一共可以成为n/i个数的约数,也即所有的约数中有n/i个i,所以扫一遍累加答案就好了. /********************************************* ...