问题描述:

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. jqGrid简单介绍

    一.要引用的文件 要使用jqGrid,首先页面上要引入如下css与js文件. 1.css <link href="/css/ui.jqgrid.css" rel=" ...

  2. unicode随笔小计

    科普字符集: ascii:一个字节,占8位,(0000 0000 - 1111 1111) 如果只是英语那就没什么问题. 后来,不同的语言有了编码诞生.为了统一,出现一个大集合.便有了. unicod ...

  3. Postman用法简介-Http请求模拟工具

    在我们平时开发中,特别是需要与接口打交道时,无论是写接口还是用接口,拿到接口后肯定都得提前测试一下,这样的话就非常需要有一个比较给力的Http请求模拟工具,现在流行的这种工具也挺多的,像火狐浏览器插件 ...

  4. lightoj 1021 - Painful Bases 状态压缩

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1021 #include<cstring> #include<cstd ...

  5. poj 1321 棋盘问题【dfs】

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28308   Accepted: 13996 Descriptio ...

  6. c#基础编程—泛型

    一.引言 泛型的主要思想是将算法与数据结构完全分离开,使得一次定义的算法能作用于多种数据结构,从而实现高度可重用的开发.泛型,通过参数类型化来实现在同一份代码中操作多种数据类型,利用“参数化类型”将类 ...

  7. PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码

    PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...

  8. C# 打开PPT文件另存为PPTX

    /// <summary> /// rename PPT /// </summary> private static void renamePPT() { //add refe ...

  9. 【linux驱动分析】ioctl函数的使用

    一.用户空间的ioctl     int  ioctl(int fd, unsigned long cmd, void *data); 第一个參数是文件描写叙述符,第二个參数代表传递的命令,它会原样传 ...

  10. mysql查看端口

    在你的my.ini(Windows)或my.cfg(Linux) 中就有啊. 或者如果已经连入MySQL可以直接 SQL code ? 1 2 3 4 5 6 7 8 9 mysql> show ...