【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. Notepad++—设置背景颜色

    之前,编程一直用的都是黑色背景色,最近发现,黑色背景色+高光字体,时间久了对眼睛特别不好.感觉自己编程到现在几年时间,眼睛就很不舒服,甚至有青光眼的趋势.所以,改用白底黑字,即"日间模式&q ...

  2. Linux搭建rsync备份服务器备份

    环境: 1台rsync备份服务器,IP:10.0.0.188 1台rsync备份客户端,IP:10.0.0.51 备份数据需注意: 1. 在业务低谷时间进行备份 2. 进行备份限速 一.搭建rsync ...

  3. Linux 参数代换 命令 xargs

    xargs 命令也是管道命令中的一员.xargs命令的功能简单来说就是参数代换.那么什么叫做参数代换,这里首先要了解管道的概念.在 linux管道 命令一节中我们详细介绍了管道命令的概念.这里我们只是 ...

  4. 2021广东工业大学十月月赛 F-hnjhd爱序列

    题目:GDUTOJ | hnjhd爱序列 (gdutcode.cn) 一开始是用双指针从尾至头遍历,但发现会tle!! 后来朋友@77给出了一种用桶的做法,相当于是用空间换时间了. 其中用到的一个原理 ...

  5. 写一个简单的AIDL

    1.首先创建一个AIDL文件,并添加上两个接口.IMyAidlInterface.aidlpackage com.example.broadcastdemo;// Declare any non-de ...

  6. GO Exit Fatal panic

    Exit() 应用程序(不只是函数)退出执行 defer 不会被执行(因为程序都退出了) log.Fatal() 输出打印内容 应用程序退出 defer 不会被执行 panic() 函数停止执行(不是 ...

  7. LINUX 系统性能检测工具vmstat

    vmstat 有2个参数,第一个是采样时间间隔(单位是s),第二个参数是采样个数. #表示 2s采样一次,一共采样2次 vmstat 2 2 也可以只写第一个参数,让系统一直采样直到停止(ctrl + ...

  8. redis 之 集群

    #:下载源码包,并编译安装 [root@localhost src]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz [root ...

  9. tomcat之nginx调度

    # :安装nginx [root@nginx ~]# yum install nginx -y #配置 [root@nginx ~]# vim /etc/nginx/nginx.conf upstre ...

  10. HashMap 和 HashSet

    对于HashSet而言,系统采用Hash算法决定集合元素的存储位置,这样可以保证快速存取集合元素: 对于HashMap,系统将value当成key的附属,系统根据Hash算法来决定key的存储位置,这 ...