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.

Solution:

The brute force solution costs O(n^2), I find a O(n) and one round solution. If gas[i] >= cost[i], then I mark it, and calcuate the gas in tank along next node, if it is less than zero, then restart search.

     int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int curr_gas = ;
int idx = -;
int tmp_gas = ;
for(int i = ; i < gas.size(); i ++) {
curr_gas += gas[i] - cost[i];
if( idx != -)
tmp_gas += gas[i] - cost[i]; if(tmp_gas < ) {
idx = -;
tmp_gas = ;
} if(gas[i] - cost[i] >= && idx == -) {
idx = i;
tmp_gas += gas[i] - cost[i];
}
} if(curr_gas >= )
return idx;
else
return -;
}

Gas Station [LeetCode]的更多相关文章

  1. 134. Gas Station leetcode

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

  2. Gas Station——LeetCode

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

  3. Gas Station leetcode java

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

  4. Gas Station|leetcode 贪心

    贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...

  5. Gas Station [leetcode] 两个解决方案

    因为gas的总数大于cost总时间.你将能够圈住整个城市. 第一溶液: 如果一開始有足够的油.从位置i出发.到位置k时剩余的油量为L(i,k). 对随意的k.L(i,k)依据i的不同,仅仅相差常数. ...

  6. [LeetCode] 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] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

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

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

  9. [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. DataTable或者DataRow转换对象

    public static IEnumerable<T> ConvertObject<T>(DataTable dt) where T : new() { var v = ty ...

  2. SIP进行时

    一直以“简单”著称的SIP其实也没那么简单,不过任何事物想掌握它都很困难. 这篇文档旨在不断的记录SIP使用过程中遇到的各种疑惑和问题. 一.响应422 Session Interval Too Sm ...

  3. git commit时message的问题

    1: 在执行git commit的时候,有两种办法为该commit添加message信息一种是git commit -m 'your message'另一种是git commit会打开commit-e ...

  4. 观摩制作小游戏(js应用)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Android 杂记 - 存货盘点用的客户端

    最近有个盘点用的东西,要放到移动设备,本来用 .Net Compact Framework,CAB 部署在 CE 系统的移动条码设备.技术太旧,我用了这个周末两天时间,把这东西在试试实现在安卓上面,给 ...

  6. Metasploit辅助模块

    msf > show auxiliary Auxiliary ========= Name                                                  Di ...

  7. 数据词典与ABAP类型映射

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. STORM_0003_linux_zookeeper_storm_遇到的几个问题

    1. 首先是花费时间在windows的eclipse下面安装fatjar因为是新版的缘故,装了很久才装上. 后来发现其实mvn可以打包出没有依赖的jar包 2. 然后是按照在ubuntu环境中的mvn ...

  9. OnClientClick和OnClick同时使用!

    摘自:http://www.cnblogs.com/zhuiyi/archive/2011/07/04/2097804.html 其实有的时候需要客户端和服务端双重校验,为什么这么说呢? 比果说,一个 ...

  10. ccc

    课本第291页第4题 #include<stdio.h> void main() { int n, m, i, k; int p_begin; ]; scanf("%d" ...