[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 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 in the clockwise direction, otherwise return -1.
Note:
- If there exists a solution, it is guaranteed to be unique.
- Both input arrays are non-empty and have the same length.
- Each element in the input arrays is a non-negative integer.
N个加油站一个圈,加油gas,耗费cost。
要求选一个加油站出发,可以开车经过所有加油站。
数据特点:具有唯一解,全正数,非空,等长。
【思路】
基于以下两个很机智的结论
1、若第i个加油站,gas[i]-cost[i]<0,必不能从这里出发。
2、因为加油站是一个圈,若ΣΔ(gas[i]-cost[i])<0无解。
【代码】
- class Solution {
- public int canCompleteCircuit(int[] gas, int[] cost) {
- int tank=0;//当前站出发后Δ装油量
- int start=0;//出发站点
- int total=0;//循环总Δ油量
- for(int i=0;i<gas.length;i++){
- int delta=gas[i]-cost[i];
- tank+=delta;
- total+=delta;
- if(tank<0){
- tank=0;
- start=i+1;
- //站点i出发为负,必然只能从下一站点出发
- }
- }
- return total<0?-1:start;
- }
- }
【举例】
Example 1:
- Input:
- gas = [1,2,3,4,5]
- cost = [3,4,5,1,2]
- Output: 3
- Explanation:
- Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
- Travel to station 4. Your tank = 4 - 1 + 5 = 8
- Travel to station 0. Your tank = 8 - 2 + 1 = 7
- Travel to station 1. Your tank = 7 - 3 + 2 = 6
- Travel to station 2. Your tank = 6 - 4 + 3 = 5
- Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
- Therefore, return 3 as the starting index.
[Leetcode 134]汽车加油站 Gas Station (环形)的更多相关文章
- [Swift]LeetCode134. 加油站 | Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [leetcode]134. Gas Station加油站
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...
- [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 ...
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- [leetcode greedy]134. Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 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 ...
随机推荐
- 0006-20180422-自动化第七章-python基础学习笔记
内容回顾: - bytes - str 和bytes - int - str - list - tuple - 深拷贝和浅拷贝 今日内容: 数据类型 - bytes - int - str - boo ...
- idea 编译报错 未结束的字符串字面值,非法的类型开始
1.修改编码 全局编码设置: File -> Other Settings -> Default Settings->file encoding 工程编码设置: File -> ...
- tomcat启动闪退问题
tomcat的bin->setclasspath.bat文件中 将两个set加上,并且将if not “%JAVA_HOME%”放到%JRE_HOME%上面.
- kubernetes1.4新特性(一):支持sysctl命令
sysctl是一个允许改变正在运行中的Linux系统内核参数的接口.可以通过sysctl修改Linux系统内核中的TCP/IP 堆栈和虚拟内存系统的高级选项,而且不需要重新启动Linux系统,就可以实 ...
- 怎样从外网访问内网Tornado
外网访问内网Tornado 本地安装了Tornado,只能在局域网内访问,怎样从外网也能访问本地Tornado? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Tornado 默认安装 ...
- 用python完成带有进度条的圆周率计算
代码如下:import math import time scale= s,m,=, print("执行开始".center(scale//2, "-")) s ...
- Guitar Pro里自动化编辑器有什么用?如何使用?
我们在使用操作Guitar Pro来进行吉他学习创作时,会遇到下面几个问题.当我们想要改变全部小节或者某一特定小节的拍速时,就会在想,有没有什么简便工具来实现我们的想法呢?告诉大家,Guitar Pr ...
- 复旦大学2017--2018学年第一学期高等代数I期末考试情况分析
一.期末考试成绩班级前十名 郭宇城(100).魏一鸣(93).乔嘉玮(92).刘宇其(90).朱柏青(90).王成文健(90).方博越(88).熊子恺(88).张君格(88).崔镇涛(87).史书珣( ...
- Spring Boot Log4j2 日志学习
简介 Java 中比较常用的日志工具类,有: Log4j. SLF4j. Commons-logging(简称jcl). Logback. Log4j2(Log4j 升级版). Jdk Logging ...
- Learning-Python【14】:匿名函数与函数递归
一.什么是匿名函数 匿名函数就是没有名字的函数,又叫lambda表达式.用于一些简单的需要用函数去解决的问题,特点是只能在定义时使用一次,且函数体只有一行 匿名函数的定义就相当于只产生一个变量的值,而 ...