题目:

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.

Hide Tags

Greedy 

链接:  http://leetcode.com/problems/gas-station/

题解:

经典题gas station,贪婪法。设计一个局部当前的gas,一个全局的gas,当局部gas小于0时,局部gas置零,设置结果为当前index的下一个位置,此情况可能出现多次。当全局gas小于0的话说明没办法遍历所有gas站,返回-1。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if(gas == null || cost == null || gas.length == 0 || cost.length == 0 || gas.length != cost.length)
return 0;
int curGas = 0;
int totalGas = 0;
int result = 0; for(int i = 0; i < gas.length; i++){
curGas += gas[i] - cost[i];
totalGas += gas[i] - cost[i];
if(curGas < 0){
result = i + 1;
curGas = 0;
}
} if(totalGas < 0)
return - 1; return result;
}
}

Update:

public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if(gas == null || cost == null || gas.length != cost.length || gas.length == 0)
return -1;
int len = gas.length;
int totalGasRequired = 0, curGas = 0, station = 0; for(int i = 0; i < len; i++) {
totalGasRequired += gas[i] - cost[i];
curGas += gas[i] - cost[i];
if(curGas < 0) {
curGas = 0;
station = i + 1;
}
} return totalGasRequired >= 0 ? station : -1;
}
}

二刷:

Java:

public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if (gas == null || cost == null || gas.length != cost.length) return -1;
int totalCost = 0, totalGas = 0, curTank = 0, startingIndex = 0; for (int i = 0; i < gas.length; i++) {
totalGas += gas[i];
totalCost += cost[i];
curTank += (gas[i] - cost[i]);
if (curTank < 0) {
curTank = 0;
startingIndex = i + 1;
}
}
return totalGas >= totalCost ? startingIndex : -1;
}
}

测试:

134. Gas Station的更多相关文章

  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

    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 ----- java

    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 (Dynamic Programming)

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

  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

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

  8. 134. Gas Station加油站

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

  9. 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. 误解了Windows Server AppFabric

    想为自己的流程引擎找一个宿主,选择了几套方案,想先从AppFabric开始,原因主要出于以下几点: 1. 自己用过Windows Service或Form作为一些定时任务等应用的宿主,但苦于学艺不精, ...

  2. ###学习《C++ Primer》- 5

    点击查看Evernote原文. // @author: gr // @date: 2014-10-20 // @email: forgerui@gmail.com Part 5: 动态内存(第12章) ...

  3. Attribute (一)

    本文导读 1.概念 2.自定义一个 Attribute 概念       Attribute是一个特殊的类,我们知道 .NET 程序集 具有自描述的特性(由于元数据),Attribute和.NET的元 ...

  4. 14_CXF发布REST服务

    [rest服务] REST服务是一种软件架构模式,只是一种风格.REST服务采用HTTP做传输协议. REST对于HTTP的利用分为以下两种: 一.资源定位 REST要求对方资源定位更加准确,如下: ...

  5. [leetcode] 407. Trapping Rain Water II

    https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...

  6. resin的简单介绍和使用

    1.resin是一款应用服务器(application server),它自身也包含一款支持Http1.1协议的WEB服务器(web server),它也可以和其他的web服务器一起工作如IIS和Ap ...

  7. outlet删除不完全

    今天在用iOS写个计算器的时候,遇到的一个小bug,新手,太新了,不之所错. 直接上码: Terminating app due to uncaught exception 'NSUnknownKey ...

  8. 一个页面,多个flash(刚学jq插件)

    只贴js那部分哦 调用 // flash轮播图 var sumF=$('.btnTabs span').length/4; //有四个flash var flashT01=new flash($('. ...

  9. ECshop网店系统百万级商品量性能优化-简单的一些Cache内存配置

    ECshop网店系统对于产品的数据.模板.Query都可以缓存,也就是把一些商品详情页.分类页.Search页的数据经过一次访问后,用文件的形式保存下来,下次有人访问相同的页面时,不用再查数据库,直接 ...

  10. VC++2010添加菜单

    1  资源视图下面右键添加资源 选择menu 2  创建你想要的menu 3  找到CDialog::OnInitDialog();在后面添加代码. CMenu menu; menu.LoadMenu ...