问题描述:

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) { //时间复杂度为O(n)
int total=;
unsigned i,j,start;
for(i=; i<gas.size(); )
{
start = i;
int residual = gas[i]-cost[i];
total += gas[i]-cost[i]; for(j =i+; j<gas.size(); j++)
{
if(residual<)
{
i=j;
break;
}
total += gas[j]-cost[j];
residual += gas[j]-cost[j];
}
i=j;
}
return total>=? start : -;
}
};

代码二:

 class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
unsigned start = ;
int current_gas = ;
int total_gas = ;
for(unsigned i=; i<gas.size(); i++)
{
current_gas += gas[i]-cost[i];
total_gas += gas[i]-cost[i];
if(current_gas<) //从第i站出发到第i+1站很耗油
{
start = i+;
current_gas = ;
}
}
return total_gas>= ? start : -;
}
};

代码一和代码二的思想是一样的,只是形式不太一样,相比较而言,代码二可读性更好。

问题分析:

如果sum(gas)>=sum(cost),则一定存在一个合适的站点,使得从该站点出发汽车可以转一圈再返回到起始点,但是起始点的唯一性并不能保障。

比如gas=[4,5,6,7],cost=[1,2,3,4],则任一站点都可以作为起始点。

所以本题中给出Note:
The solution is guaranteed to be unique.

如果sum(gas)<sum(cost),则不存在这样的起始点,这一点很容易想到。

代码思想:

假设有n个站点:S1,S2,S3,...,Sn,当前油箱内油量为0,从S1开始,判断从S1站点能否开到S2站点,如果可以的话说明达到S2站点时汽车内油量>=0,

我们标记S1>0,表示从S1可以到达S2;否则,标记S1<0。     当Si>0时,继续判断Si+1是否大于0,当Si<0时,说明当前设置的起始点不成功,将新的起始点

设为Si+1,判断从Si+1->Si+2->...->Sn是否成功。    如果从Si+1能否到达Sn,并且sum(gas)>=sum(cost),那么Si+1就可以作为起始点。

举个例子:                                            S1, S2, S3,    S, S5, S6, S7,..., Si,  Si+1, Si+2,..., Sn             标记为绿色的表示在遍历过程中被设为起始点的站点

current_gas     |>0    >0    <0 |   >0   >0   >0   >0,...,<0 |   >0      >0 ..., >0|

|       <0         |                    <0                   |            >0              |

sum(gas13)-sum(cost13)<0

[LeetCode OJ] Gas Station的更多相关文章

  1. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  2. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  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]. You ...

  4. 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 ...

  5. 【leetcode】Gas Station

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

  6. leetcode 134. Gas Station ----- java

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

  7. LeetCode _ Gas Station

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

  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]. Y ...

  9. Java for 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 ...

随机推荐

  1. BZOJ1600: [Usaco2008 Oct]建造栅栏

    1600: [Usaco2008 Oct]建造栅栏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 825  Solved: 473[Submit][Sta ...

  2. Unity Flow distort of screen

    Shader "ScreenWater" {Properties { _MainTex ("Base (RGB)", 2D) = "white&quo ...

  3. C++引用(Reference)

    引用(Reference)是C++语言相对于C语言的又一个扩充,类似于指针,只是在声明的时候用&取代了*.引用可以看做是被引用对象的一个别名,在声明引用时,必须同时对其进行初始化.引用的声明方 ...

  4. jQuery 遍历

    .add() add() 方法将元素添加到匹配元素的集合中 .add(selector)//字符串值,表示查找供添加到匹配元素集合的元素的选择器表达式. .add(elements)//添加到匹配元素 ...

  5. hdu 4717 The Moving Points(三分+计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 说明下为啥满足三分: 设y=f(x) (x>0)表示任意两个点的距离随时间x的增长,距离y ...

  6. NYOJ 14 贪心解题报告

    会场安排问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...

  7. Hibernate自定查询返回list<?>

    /** * * @param session * :一个会话 * * @param hql * :是需要执行的hql语句, * * @param offset * 设置开始位置 * * @param ...

  8. Java学习笔记六(I/O流)

    1.介绍 在实际开发过程中经常会用到数据的输入/输出操作,本篇博客着重分析一下,java中经经常使用到的有关IO操作的类.而在java中能够将经常使用的流分为两个部分:字节流和字符流. 1.流的抽象基 ...

  9. 基于Lucene3.5.0怎样从TokenStream获得Token

    通过学习Lucene3.5.0的doc文档,对不同release版本号 lucene版本号的API修改做分析.最后找到了有价值的修改信息. LUCENE-2302: Deprecated TermAt ...

  10. Java读取WEB-INF目录下的properties配置文件

    如何在Java代码中读取WEB-INF目录下的properties配置文件,下文给出了一个解决方案. 我们习惯将一些配置信息写在配置文件中,比如将数据库的配置信息URL.User和Password写在 ...