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.

思路:

1. 是否能travel around? =>只要gas[]加总的和 - cost[]加总的和 > 0,就能够。=>需要一个INT计算gas[]-cost[]的总合

2. 如何找到起点?如果到了某一站油不够,那么说明之前节点作为起点不行,起点必须从它后面开始重新找。=>需要一个INT记录从找到的起点到目前为止的油量

时间复杂度O(n)

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

134. Gas Station (Array; DP)的更多相关文章

  1. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  2. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

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

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

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

  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. 134. Gas Station

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

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

  8. 【LeetCode】134.Gas Station

    Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...

  9. 134. Gas Station加油站

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

随机推荐

  1. AspectJ入门

    AOP的实现方式有两种: AOP框架在编译阶段,就对目标类进行修改,得到的class文件已经是被修改过的.生成静态的AOP代理类(生成*.class文件已经被改掉了,需要使用特定的编译器).以Aspe ...

  2. cookie js案例

    //存cokie function setcookie(keys,value,time){ document.cookie=keys+"="+decodeURIComponent( ...

  3. C++ 无锁队列实现

    上源码 #ifndef __GLOBAL_LOCK_FREE_QUEUE_H__ #define __GLOBAL_LOCK_FREE_QUEUE_H__ #include <atomic> ...

  4. C++官方文档-this

    #include <iostream> using namespace std; class Dummy { public: int x; Dummy() : x() { } ; Dumm ...

  5. 《汇编语言 基于x86处理器》前五章的小程序

    ▶ 书中前五章的几个小程序,基本的运算操作,使用了作者的库 Irvine32 和 Irvine64(一开始以为作者网站过期了,各网站上找到的文件大小都不一样,最后发现是要搭梯子 Orz,顺利下载).注 ...

  6. mysql 忽略某个错误 继续执行

    执行如下存储过程: CREATE  PROCEDURE `aa`()BEGINcall RealtimeData_9035();call RealtimeData_9504();call Realti ...

  7. JAVA SpringMVC + FormDate + Vue + file表单 ( 实现 js 单文件和多文件上传 )

    JS 部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  8. eclipse启动tomcat无法访问的解决方法

    转自:https://www.cnblogs.com/longshiyVip/p/4637680.html 问题:: tomcat在eclipse里面能正常启动,但在浏览器中访问http://loca ...

  9. JS 操作 file标签只上传照片

    在当前高版本浏览器里 在标签里加这个属性就够用了 accept="image/*" $('input[type="file"]').live('change', ...

  10. 取得grid单元格里刚输入的文本,未保存的文本

    取得grid单元格里刚输入的文本内容,未保存的文本,正在输入的值,正在编辑的值,编辑框 dbgrid->DataSource->DataSet->UpdateRecord(); pr ...