题目:

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. mysqlimport 导入文件到数据库命令

    mysqlimport -h 172.16.145.125 -u ocetl -pocetl test  --fields-terminated-by='|' '/home/ocetl/tmp_use ...

  2. Java_Web学习路线

  3. python 笔记(一)

    1.Python优点 简单,优雅,明确 强大的模块第三方库 易移植 面向对角 可扩展 2.缺点 代码不能加密 执行速度慢 3.变量定义 第一个字母必须是字母表中的大小写,或下划线.不能以数字为开头. ...

  4. 【html】【13】特效篇--下拉导航

    html代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  5. JSON的基本格式以及与JavaScript之间的转换

    JSON的基本格式以及与JavaScript之间的转换 近来,发现很多人写json格式的数据时,总是没有达到JSON的规范,虽然在语言要求不严格的情况下能够通过, 但是,难免会遇到这样那样的问题,到时 ...

  6. elementary os进入GUI桌面环境失败

    问题描述:电脑上安装了elementary os,重启后系统很顺利的到达了Login图形界面,在选定用户并键入正确的密码后,电脑黑屏了一至两秒钟后又回到的Login界面,一开始以为是密码输入错误了,就 ...

  7. spl_autoload_register array参数

    spl_autoload_register (PHP 5 >= 5.1.2) spl_autoload_register — 注册给定的函数作为 __autoload 的实现 说明¶ bool  ...

  8. ICBC中的路由设置

    才去的中国工商银行,用身份证叫了A0076的号,前边还有26个人,闲来无聊果断拿出手机收取wifi.有两个ssid为ICBC的路由,信道分别是1号和6号,还好不需要密码,不过一会就连接上了. 那我先上 ...

  9. JAVA获取当前日期以及将字符串转成指定格式的日期

    /* * To change this template, choose Tools | Templates * and open the template in the editor. */ pac ...

  10. nls_sort和nlssort 排序功能介绍

    nls_sort和nlssort 排序功能介绍 博客分类: oracle   ALTER SESSION SET NLS_SORT=''; 排序影响整个会话 Oracle9i之前,中文是按照二进制编码 ...