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. SVN linux端配置

    1.create a folder:     mkdir /sandbox/svn 2.create svn repository:     svnadmin create /sandbox/svn/ ...

  2. "解密"微信开放高级接口 企业如何应对

    今天(2013年10月29日)腾讯终于对外公开了微信公众平台最新的接口,一石激起千层浪,对于很多微信公众平台的运营人员来说,今天是令人兴奋的一天!微信在向申请服务号的企业开发了大量接口.用户不想输入文 ...

  3. STK 10.1.3

    2692407267@qq.com.很多其它内容请关注http://user.qzone.qq.com/2692407267 STK 10.1.3与Qualnet联合仿真的demo

  4. [LeetCode] Word Search [37]

    题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...

  5. [Angular 2] Router basic and Router Params

    When we define router in Angualr 2, we use @RouteConcfig() When we want to display component, we use ...

  6. Java经典23种设计模式之结构型模式(一)

    结构型模式包含7种:适配器模式.桥接模式.组合模式.装饰模式.外观模式.享元模式.代理模式. 本文主要介绍适配器模式和桥接模式. 一.适配器模式(Adapter) 适配器模式事实上非常easy.就像手 ...

  7. Android - Service启动机制

      以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luoshe ...

  8. 关于webstorm(phpstorm)设置了编码格式之后还是乱码的问题

    今天在使用phpstorm的时候,页面开始是设置utf-8的,一切正常.但是,当我从一个gbk页面复制了一段代码到phpstorm里面的时候,页面预览的时候,居然打不开了,显示是乱码.接着我就把复制的 ...

  9. HTML 控件和web控件 OnClientClick和OnClick OnServerClick区别

      ^_^ 本来对html控件,服务器控件的知识模模糊糊的.今天特地查了相关的知识. 下面是我写代码总结的. 这些事件   主要用于在客户端执行验证,然后决定是否执行服务端事件   (没接触之前就为此 ...

  10. XML and JSON 验证

    function ChkJson(strJson) { //判断Json格式是否正确 if (strJson == null || strJson == "") return tr ...