[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 ...
随机推荐
- day08 python之函数初识
一,什么是函数? 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print(),len( ...
- SpringMVC 图片上传,检查图片大小
使用SpringMVC+Spring 前端提交图片文件到Controller,检查上传图片大小是否符合要求 直接上代码了 1.校验图片大小 这里提供出验证的方法,用于在需要校验的地方调用 /** * ...
- DB2 错误代码
sqlcode sqlstate 说明 000 00000 SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 +012 01545 未限定的列名被解释为一个有相互关系的引用 +098 0 ...
- undefined 和null的区别
undefined 和null的区别null是一个表示"无"的对象,转为数值时为0:undefined是一个表示"无"的原始值,转为数值时为NaN.undefi ...
- Unable to execute 'doFinal' with cipher instance
今天项目启动后登录项目,突然爆出Unable to execute 'doFinal' with cipher instance错误.清除cookie登录测试,又不报错了,以前也见过类似问题,因为不影 ...
- 树莓派3B安装LEDE
本来想安装openwrt的,但是op官方没有支持pi3,甚至op都不怎么发新版了,仅LEDE分支有缓慢的更新..离题了,之前给pi3装过LEDE,体验不是很好.今天到openwrt官网看了下,发现之前 ...
- [C++ Primer Plus] 第4章、复合类型(一)程序清单——指针new和delete
程序清单4.1 #include<iostream> using namespace std; void main(){ ]; yams[]=; yams[]=; yams[]=; ]={ ...
- 剑指offer(33)丑数
题目描述 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 题目分析 ...
- Codeforces 932G Palindrome Partition - 回文树 - 动态规划
题目传送门 通往???的传送点 通往神秘地带的传送点 通往未知地带的传送点 题目大意 给定一个串$s$,要求将$s$划分为$t_{1}t_{2}\cdots t_{k}$,其中$2\mid k$,且$ ...
- 解决多个div左浮动后不换行问题
问题描述:我这里有多个li 让其左浮动,并且有序没有间隙的排列,就出现了中间空隙的问题: 解决办法:让每一个的第1个元素加上 clear:both属性,我这里每一行有2个,所以是: .b li:nth ...