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. 云-AWS-百科:AWS

    ylbtech-云-AWS-百科:AWS Amazon Web Services AWS是亚马逊公司旗下云计算服务平台,为全世界范围内的客户提供云解决方案.AWS面向用户提供包括弹性计算.存储.数据库 ...

  2. 新生代老年代GC组合

    新生代通常存活时间较短,因此基于Copying算法来进行回收,所谓Copying算法就是扫描出存活的对象,并复制到一块新的完全未使用的空间中 在执行机制上JVM提供了串行GC(SerialGC).并行 ...

  3. 1033 To Fill or Not to Fill (25 分)

    1033 To Fill or Not to Fill (25 分) With highways available, driving a car from Hangzhou to any other ...

  4. OSPF里几个特殊区域(stub、Totally stubby、NSSA、Totally NSSA)总结

    网友总结: 简单的说,就是 stub过滤4,5类lsa,ABR会产生缺省的3类lsa,区域内不能引入外部路由 total stub过滤3,4,5类lsa,ABR会产生缺省的3类lsa,区域内不能引入外 ...

  5. file_get_contents是打工文件或URL获取内容的方法,比其稳定的还有curl_get_contents

    相信使用过file_get_contents函数的朋友都知道,当获取的$url访问不了时,会导致页面漫长的等待,甚至还能导致PHP进程占用CPU达100%,因此这个函数就诞生了 分享一个实际在用的函数 ...

  6. xsl如何实现递归复制?

    <xsl:template match="*" mode="addSeatSelectionToAirProduct"> <xsl:eleme ...

  7. maven package 命令报:-source1.3 中不支持注释错误

    在使用maven 打包或者编译时报:-source1.3 中不支持注释错误解决方案如下: <build>  <plugins>   <plugin>    < ...

  8. Tornado 框架的使用

    Tornado tornado是一个轻量级python的web框架,他是非阻塞式的,而且速度非常快.得利于其 非阻塞的方式和对 epoll 的运用,Tornado 每秒可以处理数以千计的连接,这意味着 ...

  9. Solr SchemaXml 一些解读

    The schema.xml file contains all of the details about which fields your documents can contain, and h ...

  10. 生产者消费者模型(Queue,JoinableQueue)

    生产者消费者模型 主要是为解耦 借助队列来实现生产者消费者模型 栈:先进后出(First In Last Out 简称 FILO) 队列: 先进先出(First In First Out 简称 FIF ...