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. 修改ORACLE的语言参数

    ALTER SYSTEM SET nls_territory = "CHINA" SCOPE=SPFILE; ALTER SYSTEM SET NLS_CURRENCY = '¥' ...

  2. JS验证手机号码

    对于表单的验证是添加信息的时候必不可少的. 下面是基于EasyUI-validatebox拓展的对手机.座机号的验证方法. PhoneAndMobile: { validator: function ...

  3. Java 编程的动态性, 第4部分: 用 Javassist 进行类转换--转载

    讲过了 Java 类格式和利用反射进行的运行时访问后,本系列到了进入更高级主题的时候了.本月我将开始本系列的第二部分,在这里 Java 类信息只不过是由应用程序操纵的另一种形式的数据结构而已.我将这个 ...

  4. img的onerror事件

    使用场景 其实on error使用上是比较简单的. 当我们网站上出现了无效图片,而我们希望用友好的方式告诉用户,而不是显示红叉叉. w3c上解释的 定义和用法: onerror 事件会在文档或图像加载 ...

  5. [JavaScript]plupload多图片上传图片

    var uploader = new plupload.Uploader({ //创建实例的构造方法     runtimes: 'html5,flash,silverlight,html4',    ...

  6. tomcat startup.sh提示java.lang.OutOfMemoryError: PermGen space

    JAVA_OPTS="-server -XX:PermSize=512M -XX:MaxPermSize=1024m"if [ -z "$LOGGING_MANAGER& ...

  7. Examples_07_06 无法下载android的sdk

    在hosts里面配置. 74.125.237.1 dl-ssl.google.com 在AndroidManifest.xml中添加 <uses-feature android:name=&qu ...

  8. Win7上IIS发布网站系统\部署项目

    1.系统已经安装IIS,如果没有安装,请到[控制面板]→[程序]→[程序和功能]→[打开或关闭Windows功能],选中Internet 信息服务Web管理工具下面的所有选项,确定:如下图 2.发布文 ...

  9. rsync从windows到linux的同步备份

    名称 角色 IP地址 Windows server 2003 服务器 Eth0:192.168.1.1 RHEL5.5 客户端 Eth0:192.168.1.2   一.cwRsyncServer服务 ...

  10. Quartz.net使用记录

    1.引入dll文件: nuget控制台:安装quartz:Install-Package Quartz 安装log4net:Install-Package log4net,这里使用log4net记录一 ...