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.

加油站问题,每个加油站可以加的油给出来,从当前当下一个加油站会消耗的汽油量给出来了,求从哪个站点出发可以循环加油站一圈。

一开始是用一个二重循环的,这样复杂度为N^2,一直TLE,代码如下:

 class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
for(int i = ; i < gas.size(); ++i){
int j = i;
int curGas = gas[j];
while(curGas >= cost[j]){
curGas -= cost[j];
j = (j+)%gas.size();
curGas += gas[j];
if(j == i)
return i;
}
}
return -;
}
};

那只能使用其他方法了,可以看出维护一个部分差的和,如果前面的部分差的和一旦小于0的话,那么可以肯定的是应该在当前节点的下一处开始,然后在维护一个整体的和,当检查部分和可以完成时,查看整体差值是否小于0就可以了,代码如下所示:

 class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int totalLeft = ;
int sum = ;
int j = -;
for(int i = ; i < gas.size(); ++i){
totalLeft += gas[i] - cost[i];
sum += gas[i] - cost[i];
if(sum < ){
j = i;
sum = ;
}
}
if(totalLeft < )
return -;
return j + ;
}
};

LeetCode OJ:Gas Station(加油站问题)的更多相关文章

  1. [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 ...

  2. [LeetCode OJ] Gas Station

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

  3. 【LeetCode】Gas Station 解题报告

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

  4. [LeetCode] 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] 134. Gas Station 解题思路

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

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

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

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

  8. 【leetcode】Gas Station

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

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

  10. LeetCode _ Gas Station

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

随机推荐

  1. linux性能分析命令1:top命令

    转载:http://www.cnblogs.com/peida/archive/2012/12/24/2831353.html top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的 ...

  2. GZFramework错误(升级修改)日志

    sqlserver下事务中处理出现为初始化selectcommand的connection属性修改CommandDataBase中的PrepareCommand方法

  3. #ifndef用法

    用于避免重复包含头文件 #ifndef _STDIO_H_ #define _STDIO_H_ ...... #endif

  4. 什么时候使用namespace

    #include<iostream.h> 不用using namespace std; #include<iostream>要用using namespace std;

  5. java quartz

     什么是Quartz Quartz是一个完全由Java编写的开源作业调度框架,为在Java应用程序中进行作业调度提供了简单却强大的机制.Quartz允许开发人员根据时间间隔来调度作业.它实现了作业和触 ...

  6. Java RMI 简单示例

    一.创建远程服务 1.创建 Remote 接口,MyRemote.java import java.rmi.*; public interface MyRemote extends Remote{ p ...

  7. Struts2的select使用

    struts2的select标签中,常用的有以下几个属性:(1)struts2中的select 标签中,必须设置的属性只有一个,即是list.(2)select标签的list中必须有值,不然会报错.如 ...

  8. angular指令与指令交互

    app.directive('mansory',function(){ return { controller:function($scope){ this.changed = function(){ ...

  9. 【Python】关于使用pycharm遇到只能使用unittest方式运行,无法直接选择Run

    相信大家可能都遇到过这个问题,使用pycharm直接运行脚本的时候,只能选择unittest的方式,能愁死个人

  10. vue-router的一个小实例

    非2.0的 vue2.0还有vue-router2.0的改变还是挺大的 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用.vue的单页面应用是基于 ...