题目:

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. Android沉浸式状态栏实现

    Step1:状态栏与导航栏半透明化 方法一:继承主题特定主题 在Android API 19以上可以使用****.TranslucentDecor***有关的主题,自带相应半透明效果 例如: < ...

  2. Error:(6, 0) No such property: outputDir for class: org.gradle.api.internal.project.DefaultProject_Decorated

    在学习greenDao过程中build.gradle文件中出现这个错误,找了半天不知道为什么.代码我是在git上下载的Demo,按理说应该是没问题的.到最后发现缺少了一个关键字Def // 这样有问题 ...

  3. 【JAVA】final修饰Field

    一.简介 final修饰符可以用来修饰变量.方法.类.final修饰变量时一旦被赋值就不可以改变. 二.final成员变量 成员变量是随类初始化或对象初始化而初始化的.当类初始化的时候,会给类变量分配 ...

  4. JSON parser error with double quotes

    Use backslash charater \ to escape double quotes in JSON file as below example. { "myInfo" ...

  5. InstallShield : 如何查找编译后的 Merge Module存放路径

    工程菜单栏中依次选择  Tools ---> Options… ,选择 Merge Modules tab 页,如下,就会看到Merge Module的存放路径,也可以根据需求修改. Merge ...

  6. 浅谈mysql中不同事务隔离级别下数据的显示效果

    事务的概念 事 务是一组原子性的SQL查询语句,也可以被看做一个工作单元.如果数据库引擎能够成功地对数据库应用所有的查询语句,它就会执行所有查询,如果任何一条查 询语句因为崩溃或其他原因而无法执行,那 ...

  7. redis 常用操作命令

    操作相关的命令连接 quit:关闭连接(connection)auth:简单密码认证 持久化 save:将数据同步保存到磁盘bgsave:将数据异步保存到磁盘lastsave:返回上次成功将数据保存到 ...

  8. LumiSoft收取邮件(含邮件附件)

    在.NET当中利用C#发送电子邮件很简单,微软也提供了默认的实现,但是收取电子邮件的操作却并没有提供解决方案.好在有一些第三方的解决方案可供选择,来简化程序员日常项目中的开发工作. 这里我选用Lumi ...

  9. 通过LDF文件实现日志回滚将数据恢复(转)

    该方法数据库恢复(www.db-recovery.com)思路 1. 创建数据TEST 2. 创建表TEMP_01 3. 在表TEMP_01中插入100条数据 4. 备份现有的数据库 5. 再次向表T ...

  10. Ajax请求过程中显示“进度”的简单实现

    Ajax在Web应用中使用得越来越频繁.在进行Ajax调用过程中一般都具有这样的做法:显示一个GIF图片动画表明后台正在工作,同时阻止用户操作本页面(比如Ajax请求通过某个按钮触发,用户不能频繁点击 ...