[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 ...
随机推荐
- Applet程序组件与AJAX技术
Applet 定义 Applet是一种运行于Web客户端环境下的Java程序组件. 工作原理 Applet以代码的形式嵌入Web页面中,用标签<applet></applet> ...
- 20190409Liunx中计划任务及压缩归档week2_day1
计划任务介绍 我们可以通过一些设置.来让电脑定时提醒我们该做什么事了.或者我们提前设置好,告诉电脑你几点做什么几点做什么,这种我们就叫它定时任务.而遇到一些需要执行的事情或任务.我们也可以通过命令来告 ...
- vue组件传值
组件的传值(组件之间的通讯) 1.父子通信 1)父传子 传递:当子组件在父组件中当做标签使用的时候,通过给子组件绑定一个自定义属性,值为需要传递的数据 接收:在子组件内部通过props进行接收 接收的 ...
- 输出UTF-8-BOM格式的文件
File localDir = new File(localPath); if(!localDir.exists()){ localDir.mkdirs(); } File file = new Fi ...
- Python 堡垒机介绍
堡垒机说明 由于运维行业流动性很高,也为了防止有人在服务中残留后门,照成安全隐患,在这里我们使用堡垒机保证服务器管理安全. 我们知道运维人员在登陆服务时需要登陆用户,从客户端到服务端的过程中堡垒机,将 ...
- 剑指offer(30)连续子数组和的最大值
题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...
- NSIS控制面板中显示安装包的大小和禁止多个安装程序实例
转载:http://www.yhxs3344.net/jscript/nsis 转载:http://www.yhxs3344.net/archives/1292 1.控制面板中显示安装包的大小 ;需要 ...
- Learning-Python【28】:基于TCP协议通信的套接字
什么是 Socket Socket 是应用层与 TCP/IP 协议通信的中间软件抽象层,它是一组接口.在设计模式中,Socket 其实就是一个门面模式,它把复杂的 TCP/IP 协议族隐藏在 Sock ...
- Docker安装Tomcat镜像并部署web项目
一.安装Tomcat 1.查找Docker Hub上的tomcat镜像 docker search tomcat 2.拉取官方的镜像 docker pull tomcat 等待下载完毕,需要一些时间. ...
- MATLAB绘图功能(1) 二维高层绘图操作
文末源代码 部分源代码 %% 基本绘图操作 x=:*pi; y=sin(x); plot(x,y); % 第二个参数为矩阵 y1=sin(x); y2=cos(x); y3=0.002*exp(x); ...