【LeetCode】Gas Station 解题报告

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/gas-station/#/description

题目描述:

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.

Ways

首先我尝试了暴力解决,时间复杂度O(n^2),可是有个case超时了。这就说明时间复杂度不能太高,应该很可能在O(n)量级,即遍历所有加油站一次就能解决。

然后我就参考了别人的方法。首先明白两点:

  1. 如果从一个加油站不能到达下一个加油站,那么之后的所有加油站都不能到
  2. 如果能走一圈,那么所有的gas之和肯定大于等于所有cost之和

所以,在进行遍历时,一方面要记录当前测试的加油站在哪,即start;另一方面要记录在到达start加油站之前缺少的gas之和是多少。

因此只要对所有加油站从标号0开始遍历一次,在遍历时,如果当前加油站不能到达下个加油站,那么start就标记为下个加油站(因为当前加油站对此后所有加油站都不能到);记录从标号0的加油站到标号为start的加油站所有缺少的gas之和need,遍历结束时,判断车厢剩的油是否大于等于从标号0的加油站到start之间缺少的gas之和need。

如果遍历一次结束时油箱剩的gas能大于等于need,说明剩的油可以补充汽车从0跑到start缺少的gas,故返回start;否则无解,返回-1.

public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int tank = 0;
int need = 0;
int start = 0;
for (int i = 0; i < gas.length; i++) {
tank += gas[i] - cost[i];
if(tank < 0){
start = i + 1;
need += tank;
tank = 0;
}
}
return tank + need >=0 ? start : -1;
}
}

Date

2017 年 3 月 31 日

【LeetCode】Gas Station 解题报告的更多相关文章

  1. LeetCode: Gas Station 解题报告

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  2. 【leetCode百题成就】Gas Station解题报告

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

  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: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  5. [leetcode]Gas Station @ Python

    原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular rou ...

  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】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  9. [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

    LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...

随机推荐

  1. accelerate

    accelerate accelerare, accumulare和accurate共享一个含义为to的词根,后半截分别是:fast, pile up, care (关心则精确). 近/反义词: ex ...

  2. Oracle—数据库名、数据库实例名、数据库域名、数据库服务名的区别

    Oracle-数据库名.数据库实例名.数据库域名.数据库服务名的区别 一.数据库名 1.什么是数据库名       数据库名就是一个数据库的标识,就像人的身份证号一样.他用参数DB_NAME表示,如果 ...

  3. Oracle中dbms_random包详解

    Oracle之DBMS_RANDOM包详解参考自:https://www.cnblogs.com/ivictor/p/4476031.html https://www.cnblogs.com/shen ...

  4. 面试 Java 后端开发的感受

    上周,密集面试了若干位Java后端候选人,工作经验在3到5年间.我的标准其实不复杂(适用90%小小小公司,BAT等自动忽略): 第一能干活,第二Java基础要好,第三最好熟悉些分布式框架.我相信其它公 ...

  5. Maven pom.xml报错解决

    用Maven建了一个web工程,总是在pom.xml头的地方报错: 大概是: Original error: Could not transfer artifact org.hamcrest:hamc ...

  6. Oracle中分割逗号函数REGEXP_SUBSTR

    最近优化FORM中的查询条件遇到某个字段可以选取多个值的问题,思路当然就是选取时将多个值通过某个符号拼接起来,查询数据的时候将拼接后的字符串按照符号分割开,在分割逗号的时候用到了一个新的方法REGEX ...

  7. 深入理解java动态代理机制

    动态代理其实就是java.lang.reflect.Proxy类动态的根据您指定的所有接口生成一个class byte,该class会继承Proxy类,并实现所有你指定的接口(您在参数中传入的接口数组 ...

  8. spring注解-自动装配

    Spring利用依赖注入(DI)完成对IOC容器中中各个组件的依赖关系赋值 一.@Autowired 默认优先按照类型去容器中找对应的组件(applicationContext.getBean(Boo ...

  9. spring jdbc 配置数据源连接数据库

    概述 在XML配置数据源,并将数据源注册到JDBC模板 JDBC模板关联业务增删改查 在XML配置数据源 <?xml version="1.0" encoding=" ...

  10. linux网络相关命令之脚本和centos启动流程

    nice 功用:设置优先权,可以改变程序执行的优先权等级.等级的范围从-19(最高优先级)到20(最低优先级).优先级为操作系统决定cpu分配的参数,优先级越高,所可能获得的 cpu时间越长. 语法: ...