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.

思路: 类似KMP不回朔的思想。start表示开始的点,sum表示当前汽车的油量。当汽车到达汽油站i时如果不能到达下一站,则更新start直到可以使汽车能够从当前节点到达下一站,如果不存在,则把start设置为下一站。

class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int len = gas.size();
if(cost.size() != len) return -;
vector<int> flag(len*, );
for(int i = ; i< len; i++)
flag[i] = gas[i] - cost[i];
for(int i = len ; i< len *; ++i)
flag[i] = flag[i-len]; int start = , sum = ;
for(int i = ; i< len + start && start < len; )
{
sum += flag[i] ;
if(sum >= ){
++i;
continue;
}
while(sum< && start < i){
sum -= flag[start];
++start;
}
i++;
if(sum < ){
sum = ;
start = i;
} }
if(start <len)
return start;
return -; }
};

LeetCode _ 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 OJ] Gas Station

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

  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. [置顶] Putty管理私钥文件

    openssh中,ssh_keygen产生的私钥,id_rsa这种密钥putty是不认识的,必须先把它转换成ppk格式, Windows上如果你安装了git,它里面bin目录下就有ssh_keygen ...

  2. POJ1505:Copying Books(区间DP)

    Description Before the invention of book-printing, it was very hard to make a copy of a book. All th ...

  3. java GBK字符转换成为UTF-8编码字符

    import java.util.HashMap; import java.util.Map; /** * 创建日期: 2014-04-18 10:36:25 * 作者: 黄飞 * mail:huan ...

  4. [Docker] Docker Machine intro

    List all the docker machine: docker-machine ls Can check 'ip' and 'status': docker-machine ip defaul ...

  5. Sql 语句添加字段、修改字段类型、默认值语法

    Sql 语句添加字段 ,) not null --修改类型 alter Table bbs ) Sql 语句修改默认值 alter table 表名 drop constraint 约束名字 --删除 ...

  6. OD: File Vulnerabilities & Protocols & Fuzz

    IE.Office 等软件有个共同点,即用文件作为程序的主要输入,但攻击者往往会挑战程序员的假定和假设. 文件格式 Fuzz 就是利用畸形文件测试软件的稳健性,其流程一般包括: * 以一个正常文件作为 ...

  7. css的clip裁剪

    clip 属性是用来设置元素的形状.用来剪裁绝对定位元素(absolute or fixed). clip有三种取值:auto |inherit|rect.inherit是继承,ie不支持这个属性, ...

  8. 关于.net类型转换判断问题

    做项目时,发现这个问题,由于数据库中的字段可能为null,如果在.net程序直接转换时,比如 ltTime.Text =DateTime.Parse(dt.Rows[0]["m_Time&q ...

  9. linux 分区

    1.硬盘分区分为基本分区和扩展分区, 扩展分区分下去就是逻辑分区,而且逻辑分区没有数量上的限制. 2.查看linux系统分区具体情况 fdisk - l 3.查看某个目录是哪个分区下的 df /boo ...

  10. java实现字符串反转(原作有点错误,需要看下评论)

    http://blog.csdn.net/shenshen123jun/article/details/9104025