博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
134. Gas Station
阅读量:7240 次
发布时间:2019-06-29

本文共 2425 字,大约阅读时间需要 8 分钟。

题目:

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
 
 

链接:  

题解:

经典题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;    }}

 

 

测试:

转载地址:http://dkfbm.baihongyu.com/

你可能感兴趣的文章
Python随笔1
查看>>
Ubuntu16.04搭建Postfix作为SMTP服务器
查看>>
Linux——网络端口的状态netstat、ifconfig
查看>>
android 单位总结
查看>>
canvas元素简易教程(5)(大部分转自火狐,自己只写了简单的代码分析)
查看>>
ArcCore重构-生成%_offset.h文件
查看>>
关于kafka的新的group无法订阅到topic中历史消息的问题
查看>>
zp_bj_03
查看>>
Idea 实时编译 和 热部署
查看>>
如何javascript获取css中的样式
查看>>
mysql INFORMATION_SCHEMA (转)
查看>>
多线程之异步编程: 经典和最新的异步编程模型,async与await
查看>>
length
查看>>
JDK源码阅读--HashMap
查看>>
Adroid 展开收起效果实现
查看>>
PHP:第五章——字符串转换与比较
查看>>
Thinkphp+Uploadify
查看>>
菜鸟学习WCF笔记-契约(Contract)
查看>>
注册登录系统的基本逻辑与结构——ASP.NET(C#)源代码
查看>>
AC日记——元素查找 codevs 1230
查看>>