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. 最近经历的一些大数据(Spark/Hadoop)面试题

    1.简答说一下hadoop的map-reduce编程模型 首先map task会从本地文件系统读取数据,转换成key-value形式的键值对集合 使用的是hadoop内置的数据类型,比如longwri ...

  2. Consul 入门

    1. 什么是Consul? Consul 有很多组件,对于整体来说,它是一个服务发现和服务配置的工具,它提供了一下特性: 服务发现 健康检查 KV存储 多数据中心 2.安装Consul 以下是在 Ce ...

  3. SELECT LAST_INSERT_ID() 的使用和注意事项

    SELECT LAST_INSERT_ID() 的使用和注意事项 尊重个人劳动成果,转载请注明出处: http://blog.csdn.net/czd3355/article/details/7130 ...

  4. Sqlserver中如何创建链接服务器

    链接服务器在跨数据库/跨服务器查询时非常有用(比如分布式数据库系统中),我将以图文方式详细说明如何利用SQL Server Management Studio在图形界面下创建链接服务器 方法/步骤   ...

  5. 线程阶段性总结——APM,ThreadPool,Task,TaskScheduler ,CancellationTokenSource

    不管我们使用thread,threadPool,task,还是APM异步,本质都是在使用多线程.对于新手来说,不太敢用多线程的原因,就我个人的体验来说,就是对多线程的异常捕获方式或时机缺乏了解,而一旦 ...

  6. Log4J使用实例---日志进行邮件发送或是存入数据库

    部分转摘:http://blog.csdn.net/azhao_dn/article/details/9118667 1.根类别(在类别层次结构的顶部,即全局性的日志级别) 配置根Logger,其语法 ...

  7. 浏览器发送URL的编码特性

    转载自:http://blog.csdn.net/wangjun_1218/article/details/4330244 浏览器发送URL的编码特性 尽管有很多规范URL的标准,例如RFC 3987 ...

  8. [C#] 类型学习笔记三:自定义值类型

    既前两篇之后,这一篇我们讨论通过struct 关键字自定义值类型. 在第一篇已经讨论过值类型的优势,节省空间,不会触发Gargage Collection等等. 在对性能要求比较高的场景下,通过str ...

  9. UVA129 暴力dfs,有许多值得学习的代码

    紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...

  10. mvc BundleConfig实现对Css、Js压缩打包加载

    Bundle不是.net Framework框架中的一员,使用Bundle首先要先添加引用,如下: nuget包管理--程序包管理控制台--Install-Package Microsoft.AspN ...